blob: 29ee89765ef5972599ae02e939b33aedf3757db6 [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
Alexander Hansen4fa40122025-07-30 18:26:35 +0200175 nlohmann::json::iterator exposes = boardValues.find("Exposes");
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200176 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;
Alexander Hansen4fa40122025-07-30 18:26:35 +0200186 for (nlohmann::json& item : *exposes)
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200187 {
Alexander Hansen4fa40122025-07-30 18:26:35 +0200188 postExposesRecordsToDBus(item, exposesIndex, boardNameOrig,
189 jsonPointerPath, jsonPointerPathBoard,
190 boardPath, boardType);
191 }
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200192
Alexander Hansen4fa40122025-07-30 18:26:35 +0200193 newBoards.emplace(boardPath, boardNameOrig);
194}
195
196void EntityManager::postExposesRecordsToDBus(
197 nlohmann::json& item, size_t& exposesIndex,
198 const std::string& boardNameOrig, std::string jsonPointerPath,
199 const std::string& jsonPointerPathBoard, const std::string& boardPath,
200 const std::string& boardType)
201{
202 exposesIndex++;
203 jsonPointerPath = jsonPointerPathBoard;
204 jsonPointerPath += std::to_string(exposesIndex);
205
206 auto findName = item.find("Name");
207 if (findName == item.end())
208 {
209 std::cerr << "cannot find name in field " << item << "\n";
210 return;
211 }
212 auto findStatus = item.find("Status");
213 // if status is not found it is assumed to be status = 'okay'
214 if (findStatus != item.end())
215 {
216 if (*findStatus == "disabled")
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200217 {
Alexander Hansen4fa40122025-07-30 18:26:35 +0200218 return;
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200219 }
Alexander Hansen4fa40122025-07-30 18:26:35 +0200220 }
221 auto findType = item.find("Type");
222 std::string itemType;
223 if (findType != item.end())
224 {
225 itemType = findType->get<std::string>();
226 std::regex_replace(itemType.begin(), itemType.begin(), itemType.end(),
227 illegalDbusPathRegex, "_");
228 }
229 else
230 {
231 itemType = "unknown";
232 }
233 std::string itemName = findName->get<std::string>();
234 std::regex_replace(itemName.begin(), itemName.begin(), itemName.end(),
235 illegalDbusMemberRegex, "_");
236 std::string ifacePath = boardPath;
237 ifacePath += "/";
238 ifacePath += itemName;
239
240 if (itemType == "BMC")
241 {
242 std::shared_ptr<sdbusplus::asio::dbus_interface> bmcIface =
243 dbus_interface.createInterface(
244 objServer, ifacePath, "xyz.openbmc_project.Inventory.Item.Bmc",
245 boardNameOrig);
246 dbus_interface::populateInterfaceFromJson(
247 io, systemConfiguration, jsonPointerPath, bmcIface, item, objServer,
248 getPermission(itemType));
249 }
250 else if (itemType == "System")
251 {
252 std::shared_ptr<sdbusplus::asio::dbus_interface> systemIface =
253 dbus_interface.createInterface(
254 objServer, ifacePath,
255 "xyz.openbmc_project.Inventory.Item.System", boardNameOrig);
256 dbus_interface::populateInterfaceFromJson(
257 io, systemConfiguration, jsonPointerPath, systemIface, item,
258 objServer, getPermission(itemType));
259 }
260
261 for (const auto& [name, config] : item.items())
262 {
263 jsonPointerPath = jsonPointerPathBoard;
264 jsonPointerPath.append(std::to_string(exposesIndex))
265 .append("/")
266 .append(name);
267 if (config.type() == nlohmann::json::value_t::object)
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200268 {
Alexander Hansen4fa40122025-07-30 18:26:35 +0200269 std::string ifaceName = "xyz.openbmc_project.Configuration.";
270 ifaceName.append(itemType).append(".").append(name);
271
272 std::shared_ptr<sdbusplus::asio::dbus_interface> objectIface =
273 dbus_interface.createInterface(objServer, ifacePath, ifaceName,
274 boardNameOrig);
275
276 dbus_interface::populateInterfaceFromJson(
277 io, systemConfiguration, jsonPointerPath, objectIface, config,
278 objServer, getPermission(name));
279 }
280 else if (config.type() == nlohmann::json::value_t::array)
281 {
282 size_t index = 0;
283 if (config.empty())
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200284 {
285 continue;
286 }
Alexander Hansen4fa40122025-07-30 18:26:35 +0200287 bool isLegal = true;
288 auto type = config[0].type();
289 if (type != nlohmann::json::value_t::object)
290 {
291 continue;
292 }
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200293
Alexander Hansen4fa40122025-07-30 18:26:35 +0200294 // verify legal json
295 for (const auto& arrayItem : config)
296 {
297 if (arrayItem.type() != type)
298 {
299 isLegal = false;
300 break;
301 }
302 }
303 if (!isLegal)
304 {
305 std::cerr << "dbus format error" << config << "\n";
306 break;
307 }
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200308
Alexander Hansen4fa40122025-07-30 18:26:35 +0200309 for (auto& arrayItem : config)
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200310 {
311 std::string ifaceName = "xyz.openbmc_project.Configuration.";
312 ifaceName.append(itemType).append(".").append(name);
Alexander Hansen4fa40122025-07-30 18:26:35 +0200313 ifaceName.append(std::to_string(index));
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200314
315 std::shared_ptr<sdbusplus::asio::dbus_interface> objectIface =
316 dbus_interface.createInterface(objServer, ifacePath,
317 ifaceName, boardNameOrig);
318
319 dbus_interface::populateInterfaceFromJson(
Alexander Hansen4fa40122025-07-30 18:26:35 +0200320 io, systemConfiguration,
321 jsonPointerPath + "/" + std::to_string(index), objectIface,
322 arrayItem, objServer, getPermission(name));
323 index++;
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200324 }
325 }
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200326 }
327
Alexander Hansen4fa40122025-07-30 18:26:35 +0200328 std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface =
329 dbus_interface.createInterface(
330 objServer, ifacePath,
331 "xyz.openbmc_project.Configuration." + itemType, boardNameOrig);
332
333 dbus_interface::populateInterfaceFromJson(
334 io, systemConfiguration, jsonPointerPath, itemIface, item, objServer,
335 getPermission(itemType));
336
337 topology.addBoard(boardPath, boardType, boardNameOrig, item);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200338}
339
Andrew Jeffery55192932022-03-24 12:29:27 +1030340static bool deviceRequiresPowerOn(const nlohmann::json& entity)
341{
342 auto powerState = entity.find("PowerState");
Andrew Jefferyb6209442022-03-24 12:36:20 +1030343 if (powerState == entity.end())
Andrew Jeffery55192932022-03-24 12:29:27 +1030344 {
Andrew Jefferyb6209442022-03-24 12:36:20 +1030345 return false;
Andrew Jeffery55192932022-03-24 12:29:27 +1030346 }
347
Ed Tanous3013fb42022-07-09 08:27:06 -0700348 const auto* ptr = powerState->get_ptr<const std::string*>();
349 if (ptr == nullptr)
Andrew Jefferyb6209442022-03-24 12:36:20 +1030350 {
351 return false;
352 }
353
354 return *ptr == "On" || *ptr == "BiosPost";
Andrew Jeffery55192932022-03-24 12:29:27 +1030355}
356
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030357static void pruneDevice(const nlohmann::json& systemConfiguration,
358 const bool powerOff, const bool scannedPowerOff,
359 const std::string& name, const nlohmann::json& device)
360{
361 if (systemConfiguration.contains(name))
362 {
363 return;
364 }
365
Andrew Jeffery4db38bc2022-03-24 13:42:41 +1030366 if (deviceRequiresPowerOn(device) && (powerOff || scannedPowerOff))
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030367 {
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030368 return;
369 }
370
371 logDeviceRemoved(device);
372}
373
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200374void EntityManager::startRemovedTimer(boost::asio::steady_timer& timer,
375 nlohmann::json& systemConfiguration)
James Feist1df06a42019-04-11 14:23:04 -0700376{
James Feistfb00f392019-06-25 14:16:48 -0700377 if (systemConfiguration.empty() || lastJson.empty())
378 {
379 return; // not ready yet
380 }
James Feist1df06a42019-04-11 14:23:04 -0700381 if (scannedPowerOn)
382 {
383 return;
384 }
385
Alexander Hansen0ab32b32025-06-27 14:50:33 +0200386 if (!powerStatus.isPowerOn() && scannedPowerOff)
James Feist1df06a42019-04-11 14:23:04 -0700387 {
388 return;
389 }
390
James Feistb1728ca2020-04-30 15:40:55 -0700391 timer.expires_after(std::chrono::seconds(10));
Andrew Jeffery27a1cfb2022-03-24 12:31:53 +1030392 timer.async_wait(
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200393 [&systemConfiguration, this](const boost::system::error_code& ec) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400394 if (ec == boost::asio::error::operation_aborted)
395 {
396 return;
397 }
Andrew Jeffery27a1cfb2022-03-24 12:31:53 +1030398
Alexander Hansen0ab32b32025-06-27 14:50:33 +0200399 bool powerOff = !powerStatus.isPowerOn();
Patrick Williamsb7077432024-08-16 15:22:21 -0400400 for (const auto& [name, device] : lastJson.items())
401 {
402 pruneDevice(systemConfiguration, powerOff, scannedPowerOff,
403 name, device);
404 }
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030405
Patrick Williamsb7077432024-08-16 15:22:21 -0400406 scannedPowerOff = true;
407 if (!powerOff)
408 {
409 scannedPowerOn = true;
410 }
411 });
James Feist1df06a42019-04-11 14:23:04 -0700412}
413
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200414void EntityManager::pruneConfiguration(bool powerOff, const std::string& name,
415 const nlohmann::json& device)
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030416{
417 if (powerOff && deviceRequiresPowerOn(device))
418 {
419 // power not on yet, don't know if it's there or not
420 return;
421 }
422
Alexander Hansen57604ed2025-06-27 13:22:28 +0200423 auto& ifaces = dbus_interface.getDeviceInterfaces(device);
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030424 for (auto& iface : ifaces)
425 {
426 auto sharedPtr = iface.lock();
427 if (!!sharedPtr)
428 {
429 objServer.remove_interface(sharedPtr);
430 }
431 }
432
433 ifaces.clear();
434 systemConfiguration.erase(name);
Matt Spinler6eb60972023-08-14 16:36:20 -0500435 topology.remove(device["Name"].get<std::string>());
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030436 logDeviceRemoved(device);
437}
438
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200439void EntityManager::publishNewConfiguration(
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030440 const size_t& instance, const size_t count,
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200441 boost::asio::steady_timer& timer, // Gerrit discussion:
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030442 // https://gerrit.openbmc-project.xyz/c/openbmc/entity-manager/+/52316/6
443 //
444 // Discord discussion:
445 // https://discord.com/channels/775381525260664832/867820390406422538/958048437729910854
446 //
447 // NOLINTNEXTLINE(performance-unnecessary-value-param)
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200448 const nlohmann::json newConfiguration)
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030449{
Alexander Hansena555acf2025-06-27 11:59:10 +0200450 loadOverlays(newConfiguration, io);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030451
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200452 boost::asio::post(io, [this]() {
Christopher Meisf7252572025-06-11 13:22:05 +0200453 if (!writeJsonFiles(systemConfiguration))
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030454 {
455 std::cerr << "Error writing json files\n";
456 }
457 });
458
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200459 boost::asio::post(io, [this, &instance, count, &timer, newConfiguration]() {
460 postToDbus(newConfiguration);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030461 if (count == instance)
462 {
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200463 startRemovedTimer(timer, systemConfiguration);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030464 }
465 });
466}
467
James Feist8f2710a2018-05-09 17:18:55 -0700468// main properties changed entry
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200469void EntityManager::propertiesChangedCallback()
James Feist8f2710a2018-05-09 17:18:55 -0700470{
Alexander Hansenfc1b1e22025-06-27 14:34:11 +0200471 propertiesChangedInstance++;
472 size_t count = propertiesChangedInstance;
James Feist1df06a42019-04-11 14:23:04 -0700473
Alexander Hansenb1340da2025-06-27 14:29:13 +0200474 propertiesChangedTimer.expires_after(std::chrono::milliseconds(500));
James Feist8f2710a2018-05-09 17:18:55 -0700475
476 // setup an async wait as we normally get flooded with new requests
Alexander Hansenb1340da2025-06-27 14:29:13 +0200477 propertiesChangedTimer.async_wait(
478 [this, count](const boost::system::error_code& ec) {
479 if (ec == boost::asio::error::operation_aborted)
480 {
481 // we were cancelled
482 return;
483 }
484 if (ec)
485 {
486 std::cerr << "async wait error " << ec << "\n";
487 return;
488 }
James Feist8f2710a2018-05-09 17:18:55 -0700489
Alexander Hansenb1340da2025-06-27 14:29:13 +0200490 if (propertiesChangedInProgress)
491 {
492 propertiesChangedCallback();
493 return;
494 }
495 propertiesChangedInProgress = true;
James Feist2539ccd2020-05-01 16:15:08 -0700496
Alexander Hansenb1340da2025-06-27 14:29:13 +0200497 nlohmann::json oldConfiguration = systemConfiguration;
498 auto missingConfigurations = std::make_shared<nlohmann::json>();
499 *missingConfigurations = systemConfiguration;
James Feist899e17f2019-09-13 11:46:29 -0700500
Alexander Hansenb1340da2025-06-27 14:29:13 +0200501 auto perfScan = std::make_shared<scan::PerformScan>(
Christopher Meisf7252572025-06-11 13:22:05 +0200502 *this, *missingConfigurations, configuration.configurations, io,
Alexander Hansenb1340da2025-06-27 14:29:13 +0200503 [this, count, oldConfiguration, missingConfigurations]() {
504 // this is something that since ac has been applied to the
505 // bmc we saw, and we no longer see it
Alexander Hansen0ab32b32025-06-27 14:50:33 +0200506 bool powerOff = !powerStatus.isPowerOn();
Alexander Hansenb1340da2025-06-27 14:29:13 +0200507 for (const auto& [name, device] :
508 missingConfigurations->items())
509 {
510 pruneConfiguration(powerOff, name, device);
511 }
Alexander Hansenb1340da2025-06-27 14:29:13 +0200512 nlohmann::json newConfiguration = systemConfiguration;
513
Christopher Meisf7252572025-06-11 13:22:05 +0200514 deriveNewConfiguration(oldConfiguration, newConfiguration);
Alexander Hansenb1340da2025-06-27 14:29:13 +0200515
516 for (const auto& [_, device] : newConfiguration.items())
517 {
518 logDeviceAdded(device);
519 }
520
521 propertiesChangedInProgress = false;
522
523 boost::asio::post(io, [this, newConfiguration, count] {
524 publishNewConfiguration(
Alexander Hansenfc1b1e22025-06-27 14:34:11 +0200525 std::ref(propertiesChangedInstance), count,
Alexander Hansenb1340da2025-06-27 14:29:13 +0200526 std::ref(propertiesChangedTimer), newConfiguration);
527 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200528 });
Alexander Hansenb1340da2025-06-27 14:29:13 +0200529 perfScan->run();
530 });
James Feist75fdeeb2018-02-20 14:26:16 -0800531}
532
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600533// Check if InterfacesAdded payload contains an iface that needs probing.
Patrick Williamsb7077432024-08-16 15:22:21 -0400534static bool iaContainsProbeInterface(
Christopher Meisf7252572025-06-11 13:22:05 +0200535 sdbusplus::message_t& msg,
536 const std::unordered_set<std::string>& probeInterfaces)
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600537{
538 sdbusplus::message::object_path path;
539 DBusObject interfaces;
540 std::set<std::string> interfaceSet;
541 std::set<std::string> intersect;
542
543 msg.read(path, interfaces);
544
545 std::for_each(interfaces.begin(), interfaces.end(),
546 [&interfaceSet](const auto& iface) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400547 interfaceSet.insert(iface.first);
548 });
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600549
550 std::set_intersection(interfaceSet.begin(), interfaceSet.end(),
551 probeInterfaces.begin(), probeInterfaces.end(),
552 std::inserter(intersect, intersect.end()));
553 return !intersect.empty();
554}
555
556// Check if InterfacesRemoved payload contains an iface that needs probing.
Patrick Williamsb7077432024-08-16 15:22:21 -0400557static bool irContainsProbeInterface(
Christopher Meisf7252572025-06-11 13:22:05 +0200558 sdbusplus::message_t& msg,
559 const std::unordered_set<std::string>& probeInterfaces)
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600560{
561 sdbusplus::message::object_path path;
562 std::set<std::string> interfaces;
563 std::set<std::string> intersect;
564
565 msg.read(path, interfaces);
566
567 std::set_intersection(interfaces.begin(), interfaces.end(),
568 probeInterfaces.begin(), probeInterfaces.end(),
569 std::inserter(intersect, intersect.end()));
570 return !intersect.empty();
571}
572
Alexander Hansenad28e402025-06-25 12:38:20 +0200573void EntityManager::handleCurrentConfigurationJson()
574{
575 if (em_utils::fwVersionIsSame())
576 {
Christopher Meisf7252572025-06-11 13:22:05 +0200577 if (std::filesystem::is_regular_file(currentConfiguration))
Alexander Hansenad28e402025-06-25 12:38:20 +0200578 {
579 // this file could just be deleted, but it's nice for debug
580 std::filesystem::create_directory(tempConfigDir);
581 std::filesystem::remove(lastConfiguration);
Christopher Meisf7252572025-06-11 13:22:05 +0200582 std::filesystem::copy(currentConfiguration, lastConfiguration);
583 std::filesystem::remove(currentConfiguration);
Alexander Hansenad28e402025-06-25 12:38:20 +0200584
585 std::ifstream jsonStream(lastConfiguration);
586 if (jsonStream.good())
587 {
588 auto data = nlohmann::json::parse(jsonStream, nullptr, false);
589 if (data.is_discarded())
590 {
591 std::cerr
592 << "syntax error in " << lastConfiguration << "\n";
593 }
594 else
595 {
596 lastJson = std::move(data);
597 }
598 }
599 else
600 {
601 std::cerr << "unable to open " << lastConfiguration << "\n";
602 }
603 }
604 }
605 else
606 {
607 // not an error, just logging at this level to make it in the journal
608 std::cerr << "Clearing previous configuration\n";
Christopher Meisf7252572025-06-11 13:22:05 +0200609 std::filesystem::remove(currentConfiguration);
Alexander Hansenad28e402025-06-25 12:38:20 +0200610 }
611}
612
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200613void EntityManager::registerCallback(const std::string& path)
James Feist75fdeeb2018-02-20 14:26:16 -0800614{
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200615 if (dbusMatches.contains(path))
616 {
617 return;
618 }
Nan Zhoua3315672022-09-20 19:48:14 +0000619
Alexander Hansen60803182025-06-27 14:41:28 +0200620 lg2::debug("creating PropertiesChanged match on {PATH}", "PATH", path);
621
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200622 std::function<void(sdbusplus::message_t & message)> eventHandler =
623 [&](sdbusplus::message_t&) { propertiesChangedCallback(); };
James Feistfd1264a2018-05-03 12:10:00 -0700624
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200625 sdbusplus::bus::match_t match(
626 static_cast<sdbusplus::bus_t&>(*systemBus),
627 "type='signal',member='PropertiesChanged',path='" + path + "'",
628 eventHandler);
629 dbusMatches.emplace(path, std::move(match));
630}
James Feistfd1264a2018-05-03 12:10:00 -0700631
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200632// We need a poke from DBus for static providers that create all their
633// objects prior to claiming a well-known name, and thus don't emit any
634// org.freedesktop.DBus.Properties signals. Similarly if a process exits
635// for any reason, expected or otherwise, we'll need a poke to remove
636// entities from DBus.
Christopher Meisf7252572025-06-11 13:22:05 +0200637void EntityManager::initFilters(
638 const std::unordered_set<std::string>& probeInterfaces)
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200639{
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200640 nameOwnerChangedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500641 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishopc76af0f2020-12-04 13:50:23 -0500642 sdbusplus::bus::match::rules::nameOwnerChanged(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200643 [this](sdbusplus::message_t& m) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400644 auto [name, oldOwner,
645 newOwner] = m.unpack<std::string, std::string, std::string>();
Patrick Williams7b8786f2022-10-10 10:23:37 -0500646
Patrick Williamsb7077432024-08-16 15:22:21 -0400647 if (name.starts_with(':'))
648 {
649 // We should do nothing with unique-name connections.
650 return;
651 }
Patrick Williams7b8786f2022-10-10 10:23:37 -0500652
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200653 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400654 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200655
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500656 // We also need a poke from DBus when new interfaces are created or
657 // destroyed.
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200658 interfacesAddedMatch = 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::interfacesAdded(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200661 [this, probeInterfaces](sdbusplus::message_t& msg) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400662 if (iaContainsProbeInterface(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
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200668 interfacesRemovedMatch = 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::interfacesRemoved(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200671 [this, probeInterfaces](sdbusplus::message_t& msg) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400672 if (irContainsProbeInterface(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}