blob: 3835cc14695e084df5162930a306d935e6021d4c [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/replace.hpp>
James Feistf5125b02019-06-06 11:27:43 -070020#include <boost/algorithm/string/split.hpp>
James Feist02d2b932020-02-06 16:28:48 -080021#include <boost/asio/io_context.hpp>
Ed Tanous49a888c2023-03-06 13:44:51 -080022#include <boost/asio/post.hpp>
James Feistb1728ca2020-04-30 15:40:55 -070023#include <boost/asio/steady_timer.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080024#include <boost/container/flat_map.hpp>
25#include <boost/container/flat_set.hpp>
James Feistf5125b02019-06-06 11:27:43 -070026#include <boost/range/iterator_range.hpp>
James Feist8c505da2020-05-28 10:06:33 -070027#include <nlohmann/json.hpp>
28#include <sdbusplus/asio/connection.hpp>
29#include <sdbusplus/asio/object_server.hpp>
30
James Feist637b3ef2019-04-15 16:35:30 -070031#include <filesystem>
James Feista465ccc2019-02-08 12:51:01 -080032#include <fstream>
Andrew Jefferye35d0ac2022-03-24 15:53:13 +103033#include <functional>
James Feista465ccc2019-02-08 12:51:01 -080034#include <iostream>
Andrew Jeffery666583b2021-12-01 15:50:12 +103035#include <map>
James Feista465ccc2019-02-08 12:51:01 -080036#include <regex>
James Feist1df06a42019-04-11 14:23:04 -070037constexpr const char* tempConfigDir = "/tmp/configuration/";
38constexpr const char* lastConfiguration = "/tmp/configuration/last.json";
James Feistf1b14142019-04-10 15:22:09 -070039
Adrian Ambrożewiczc789fca2020-05-14 15:50:05 +020040static constexpr std::array<const char*, 6> settableInterfaces = {
41 "FanProfile", "Pid", "Pid.Zone", "Stepwise", "Thresholds", "Polling"};
James Feist3cb5fec2018-01-23 14:41:51 -080042
Ed Tanous07d467b2021-02-23 14:48:37 -080043const std::regex illegalDbusPathRegex("[^A-Za-z0-9_.]");
44const std::regex illegalDbusMemberRegex("[^A-Za-z0-9_]");
James Feist1b2e2242018-01-30 13:45:19 -080045
James Feista465ccc2019-02-08 12:51:01 -080046sdbusplus::asio::PropertyPermission getPermission(const std::string& interface)
James Feistc6248a52018-08-14 10:09:45 -070047{
48 return std::find(settableInterfaces.begin(), settableInterfaces.end(),
49 interface) != settableInterfaces.end()
50 ? sdbusplus::asio::PropertyPermission::readWrite
51 : sdbusplus::asio::PropertyPermission::readOnly;
52}
53
Christopher Meiscf6a75b2025-06-03 07:53:50 +020054EntityManager::EntityManager(
Alexander Hansena555acf2025-06-27 11:59:10 +020055 std::shared_ptr<sdbusplus::asio::connection>& systemBus,
56 boost::asio::io_context& io) :
Christopher Meiscf6a75b2025-06-03 07:53:50 +020057 systemBus(systemBus),
58 objServer(sdbusplus::asio::object_server(systemBus, /*skipManager=*/true)),
59 lastJson(nlohmann::json::object()),
Alexander Hansenb1340da2025-06-27 14:29:13 +020060 systemConfiguration(nlohmann::json::object()), io(io),
Alexander Hansen89737252025-08-04 15:15:13 +020061 dbus_interface(io, objServer), powerStatus(*systemBus),
62 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] :
Alexander Hansen32e74182025-08-20 16:16:00 +020091 topology.getAssocs(std::views::keys(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(
Alexander Hansen89737252025-08-04 15:15:13 +0200100 assocPath, "xyz.openbmc_project.Association.Definitions",
Matt Spinler6eb60972023-08-14 16:36:20 -0500101 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 =
Alexander Hansen89737252025-08-04 15:15:13 +0200138 dbus_interface.createInterface(
139 boardPath, "xyz.openbmc_project.Inventory.Item", boardName);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200140
141 std::shared_ptr<sdbusplus::asio::dbus_interface> boardIface =
142 dbus_interface.createInterface(
Alexander Hansen89737252025-08-04 15:15:13 +0200143 boardPath, "xyz.openbmc_project.Inventory.Item." + boardType,
144 boardNameOrig);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200145
Alexander Hansen89737252025-08-04 15:15:13 +0200146 dbus_interface.createAddObjectMethod(jsonPointerPath, boardPath,
147 systemConfiguration, boardNameOrig);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200148
Alexander Hansen89737252025-08-04 15:15:13 +0200149 dbus_interface.populateInterfaceFromJson(
150 systemConfiguration, jsonPointerPath, boardIface, boardValues);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200151 jsonPointerPath += "/";
152 // iterate through board properties
153 for (const auto& [propName, propValue] : boardValues.items())
154 {
155 if (propValue.type() == nlohmann::json::value_t::object)
156 {
157 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
Alexander Hansen89737252025-08-04 15:15:13 +0200158 dbus_interface.createInterface(boardPath, propName,
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200159 boardNameOrig);
160
Alexander Hansen89737252025-08-04 15:15:13 +0200161 dbus_interface.populateInterfaceFromJson(
162 systemConfiguration, jsonPointerPath + propName, iface,
163 propValue);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200164 }
165 }
166
Alexander Hansen4fa40122025-07-30 18:26:35 +0200167 nlohmann::json::iterator exposes = boardValues.find("Exposes");
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200168 if (exposes == boardValues.end())
169 {
170 return;
171 }
172 // iterate through exposes
173 jsonPointerPath += "Exposes/";
174
175 // store the board level pointer so we can modify it on the way down
176 std::string jsonPointerPathBoard = jsonPointerPath;
177 size_t exposesIndex = -1;
Alexander Hansen4fa40122025-07-30 18:26:35 +0200178 for (nlohmann::json& item : *exposes)
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200179 {
Alexander Hansen4fa40122025-07-30 18:26:35 +0200180 postExposesRecordsToDBus(item, exposesIndex, boardNameOrig,
181 jsonPointerPath, jsonPointerPathBoard,
182 boardPath, boardType);
183 }
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200184
Alexander Hansen4fa40122025-07-30 18:26:35 +0200185 newBoards.emplace(boardPath, boardNameOrig);
186}
187
188void EntityManager::postExposesRecordsToDBus(
189 nlohmann::json& item, size_t& exposesIndex,
190 const std::string& boardNameOrig, std::string jsonPointerPath,
191 const std::string& jsonPointerPathBoard, const std::string& boardPath,
192 const std::string& boardType)
193{
194 exposesIndex++;
195 jsonPointerPath = jsonPointerPathBoard;
196 jsonPointerPath += std::to_string(exposesIndex);
197
198 auto findName = item.find("Name");
199 if (findName == item.end())
200 {
201 std::cerr << "cannot find name in field " << item << "\n";
202 return;
203 }
204 auto findStatus = item.find("Status");
205 // if status is not found it is assumed to be status = 'okay'
206 if (findStatus != item.end())
207 {
208 if (*findStatus == "disabled")
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200209 {
Alexander Hansen4fa40122025-07-30 18:26:35 +0200210 return;
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200211 }
Alexander Hansen4fa40122025-07-30 18:26:35 +0200212 }
213 auto findType = item.find("Type");
214 std::string itemType;
215 if (findType != item.end())
216 {
217 itemType = findType->get<std::string>();
218 std::regex_replace(itemType.begin(), itemType.begin(), itemType.end(),
219 illegalDbusPathRegex, "_");
220 }
221 else
222 {
223 itemType = "unknown";
224 }
225 std::string itemName = findName->get<std::string>();
226 std::regex_replace(itemName.begin(), itemName.begin(), itemName.end(),
227 illegalDbusMemberRegex, "_");
228 std::string ifacePath = boardPath;
229 ifacePath += "/";
230 ifacePath += itemName;
231
232 if (itemType == "BMC")
233 {
234 std::shared_ptr<sdbusplus::asio::dbus_interface> bmcIface =
235 dbus_interface.createInterface(
Alexander Hansen89737252025-08-04 15:15:13 +0200236 ifacePath, "xyz.openbmc_project.Inventory.Item.Bmc",
Alexander Hansen4fa40122025-07-30 18:26:35 +0200237 boardNameOrig);
Alexander Hansen89737252025-08-04 15:15:13 +0200238 dbus_interface.populateInterfaceFromJson(
239 systemConfiguration, jsonPointerPath, bmcIface, item,
Alexander Hansen4fa40122025-07-30 18:26:35 +0200240 getPermission(itemType));
241 }
242 else if (itemType == "System")
243 {
244 std::shared_ptr<sdbusplus::asio::dbus_interface> systemIface =
245 dbus_interface.createInterface(
Alexander Hansen89737252025-08-04 15:15:13 +0200246 ifacePath, "xyz.openbmc_project.Inventory.Item.System",
247 boardNameOrig);
248 dbus_interface.populateInterfaceFromJson(
249 systemConfiguration, jsonPointerPath, systemIface, item,
250 getPermission(itemType));
Alexander Hansen4fa40122025-07-30 18:26:35 +0200251 }
252
253 for (const auto& [name, config] : item.items())
254 {
255 jsonPointerPath = jsonPointerPathBoard;
256 jsonPointerPath.append(std::to_string(exposesIndex))
257 .append("/")
258 .append(name);
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200259
260 if (!postConfigurationRecord(name, config, boardNameOrig, itemType,
261 jsonPointerPath, ifacePath))
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200262 {
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200263 break;
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200264 }
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200265 }
266
Alexander Hansen4fa40122025-07-30 18:26:35 +0200267 std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface =
268 dbus_interface.createInterface(
Alexander Hansen89737252025-08-04 15:15:13 +0200269 ifacePath, "xyz.openbmc_project.Configuration." + itemType,
270 boardNameOrig);
Alexander Hansen4fa40122025-07-30 18:26:35 +0200271
Alexander Hansen89737252025-08-04 15:15:13 +0200272 dbus_interface.populateInterfaceFromJson(
273 systemConfiguration, jsonPointerPath, itemIface, item,
Alexander Hansen4fa40122025-07-30 18:26:35 +0200274 getPermission(itemType));
275
276 topology.addBoard(boardPath, boardType, boardNameOrig, item);
Alexander Hansen4fcd9462025-07-30 17:44:44 +0200277}
278
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200279bool EntityManager::postConfigurationRecord(
280 const std::string& name, nlohmann::json& config,
281 const std::string& boardNameOrig, const std::string& itemType,
282 const std::string& jsonPointerPath, const std::string& ifacePath)
283{
284 if (config.type() == nlohmann::json::value_t::object)
285 {
286 std::string ifaceName = "xyz.openbmc_project.Configuration.";
287 ifaceName.append(itemType).append(".").append(name);
288
289 std::shared_ptr<sdbusplus::asio::dbus_interface> objectIface =
Alexander Hansen89737252025-08-04 15:15:13 +0200290 dbus_interface.createInterface(ifacePath, ifaceName, boardNameOrig);
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200291
Alexander Hansen89737252025-08-04 15:15:13 +0200292 dbus_interface.populateInterfaceFromJson(
293 systemConfiguration, jsonPointerPath, objectIface, config,
294 getPermission(name));
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200295 }
296 else if (config.type() == nlohmann::json::value_t::array)
297 {
298 size_t index = 0;
299 if (config.empty())
300 {
301 return true;
302 }
303 bool isLegal = true;
304 auto type = config[0].type();
305 if (type != nlohmann::json::value_t::object)
306 {
307 return true;
308 }
309
310 // verify legal json
311 for (const auto& arrayItem : config)
312 {
313 if (arrayItem.type() != type)
314 {
315 isLegal = false;
316 break;
317 }
318 }
319 if (!isLegal)
320 {
321 std::cerr << "dbus format error" << config << "\n";
322 return false;
323 }
324
325 for (auto& arrayItem : config)
326 {
327 std::string ifaceName = "xyz.openbmc_project.Configuration.";
328 ifaceName.append(itemType).append(".").append(name);
329 ifaceName.append(std::to_string(index));
330
331 std::shared_ptr<sdbusplus::asio::dbus_interface> objectIface =
Alexander Hansen89737252025-08-04 15:15:13 +0200332 dbus_interface.createInterface(ifacePath, ifaceName,
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200333 boardNameOrig);
334
Alexander Hansen89737252025-08-04 15:15:13 +0200335 dbus_interface.populateInterfaceFromJson(
336 systemConfiguration,
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200337 jsonPointerPath + "/" + std::to_string(index), objectIface,
Alexander Hansen89737252025-08-04 15:15:13 +0200338 arrayItem, getPermission(name));
Alexander Hansen5aa65d82025-07-31 14:08:42 +0200339 index++;
340 }
341 }
342
343 return true;
344}
345
Andrew Jeffery55192932022-03-24 12:29:27 +1030346static bool deviceRequiresPowerOn(const nlohmann::json& entity)
347{
348 auto powerState = entity.find("PowerState");
Andrew Jefferyb6209442022-03-24 12:36:20 +1030349 if (powerState == entity.end())
Andrew Jeffery55192932022-03-24 12:29:27 +1030350 {
Andrew Jefferyb6209442022-03-24 12:36:20 +1030351 return false;
Andrew Jeffery55192932022-03-24 12:29:27 +1030352 }
353
Ed Tanous3013fb42022-07-09 08:27:06 -0700354 const auto* ptr = powerState->get_ptr<const std::string*>();
355 if (ptr == nullptr)
Andrew Jefferyb6209442022-03-24 12:36:20 +1030356 {
357 return false;
358 }
359
360 return *ptr == "On" || *ptr == "BiosPost";
Andrew Jeffery55192932022-03-24 12:29:27 +1030361}
362
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030363static void pruneDevice(const nlohmann::json& systemConfiguration,
364 const bool powerOff, const bool scannedPowerOff,
365 const std::string& name, const nlohmann::json& device)
366{
367 if (systemConfiguration.contains(name))
368 {
369 return;
370 }
371
Andrew Jeffery4db38bc2022-03-24 13:42:41 +1030372 if (deviceRequiresPowerOn(device) && (powerOff || scannedPowerOff))
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030373 {
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030374 return;
375 }
376
377 logDeviceRemoved(device);
378}
379
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200380void EntityManager::startRemovedTimer(boost::asio::steady_timer& timer,
381 nlohmann::json& systemConfiguration)
James Feist1df06a42019-04-11 14:23:04 -0700382{
James Feistfb00f392019-06-25 14:16:48 -0700383 if (systemConfiguration.empty() || lastJson.empty())
384 {
385 return; // not ready yet
386 }
James Feist1df06a42019-04-11 14:23:04 -0700387 if (scannedPowerOn)
388 {
389 return;
390 }
391
Alexander Hansen0ab32b32025-06-27 14:50:33 +0200392 if (!powerStatus.isPowerOn() && scannedPowerOff)
James Feist1df06a42019-04-11 14:23:04 -0700393 {
394 return;
395 }
396
James Feistb1728ca2020-04-30 15:40:55 -0700397 timer.expires_after(std::chrono::seconds(10));
Andrew Jeffery27a1cfb2022-03-24 12:31:53 +1030398 timer.async_wait(
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200399 [&systemConfiguration, this](const boost::system::error_code& ec) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400400 if (ec == boost::asio::error::operation_aborted)
401 {
402 return;
403 }
Andrew Jeffery27a1cfb2022-03-24 12:31:53 +1030404
Alexander Hansen0ab32b32025-06-27 14:50:33 +0200405 bool powerOff = !powerStatus.isPowerOn();
Patrick Williamsb7077432024-08-16 15:22:21 -0400406 for (const auto& [name, device] : lastJson.items())
407 {
408 pruneDevice(systemConfiguration, powerOff, scannedPowerOff,
409 name, device);
410 }
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030411
Patrick Williamsb7077432024-08-16 15:22:21 -0400412 scannedPowerOff = true;
413 if (!powerOff)
414 {
415 scannedPowerOn = true;
416 }
417 });
James Feist1df06a42019-04-11 14:23:04 -0700418}
419
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200420void EntityManager::pruneConfiguration(bool powerOff, const std::string& name,
421 const nlohmann::json& device)
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030422{
423 if (powerOff && deviceRequiresPowerOn(device))
424 {
425 // power not on yet, don't know if it's there or not
426 return;
427 }
428
Alexander Hansen57604ed2025-06-27 13:22:28 +0200429 auto& ifaces = dbus_interface.getDeviceInterfaces(device);
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030430 for (auto& iface : ifaces)
431 {
432 auto sharedPtr = iface.lock();
433 if (!!sharedPtr)
434 {
435 objServer.remove_interface(sharedPtr);
436 }
437 }
438
439 ifaces.clear();
440 systemConfiguration.erase(name);
Matt Spinler6eb60972023-08-14 16:36:20 -0500441 topology.remove(device["Name"].get<std::string>());
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030442 logDeviceRemoved(device);
443}
444
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200445void EntityManager::publishNewConfiguration(
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030446 const size_t& instance, const size_t count,
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200447 boost::asio::steady_timer& timer, // Gerrit discussion:
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030448 // https://gerrit.openbmc-project.xyz/c/openbmc/entity-manager/+/52316/6
449 //
450 // Discord discussion:
451 // https://discord.com/channels/775381525260664832/867820390406422538/958048437729910854
452 //
453 // NOLINTNEXTLINE(performance-unnecessary-value-param)
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200454 const nlohmann::json newConfiguration)
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030455{
Alexander Hansena555acf2025-06-27 11:59:10 +0200456 loadOverlays(newConfiguration, io);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030457
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200458 boost::asio::post(io, [this]() {
Christopher Meisf7252572025-06-11 13:22:05 +0200459 if (!writeJsonFiles(systemConfiguration))
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030460 {
461 std::cerr << "Error writing json files\n";
462 }
463 });
464
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200465 boost::asio::post(io, [this, &instance, count, &timer, newConfiguration]() {
466 postToDbus(newConfiguration);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030467 if (count == instance)
468 {
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200469 startRemovedTimer(timer, systemConfiguration);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030470 }
471 });
472}
473
James Feist8f2710a2018-05-09 17:18:55 -0700474// main properties changed entry
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200475void EntityManager::propertiesChangedCallback()
James Feist8f2710a2018-05-09 17:18:55 -0700476{
Alexander Hansenfc1b1e22025-06-27 14:34:11 +0200477 propertiesChangedInstance++;
478 size_t count = propertiesChangedInstance;
James Feist1df06a42019-04-11 14:23:04 -0700479
Alexander Hansenb1340da2025-06-27 14:29:13 +0200480 propertiesChangedTimer.expires_after(std::chrono::milliseconds(500));
James Feist8f2710a2018-05-09 17:18:55 -0700481
482 // setup an async wait as we normally get flooded with new requests
Alexander Hansenb1340da2025-06-27 14:29:13 +0200483 propertiesChangedTimer.async_wait(
484 [this, count](const boost::system::error_code& ec) {
485 if (ec == boost::asio::error::operation_aborted)
486 {
487 // we were cancelled
488 return;
489 }
490 if (ec)
491 {
492 std::cerr << "async wait error " << ec << "\n";
493 return;
494 }
James Feist8f2710a2018-05-09 17:18:55 -0700495
Alexander Hansenb1340da2025-06-27 14:29:13 +0200496 if (propertiesChangedInProgress)
497 {
498 propertiesChangedCallback();
499 return;
500 }
501 propertiesChangedInProgress = true;
James Feist2539ccd2020-05-01 16:15:08 -0700502
Alexander Hansenb1340da2025-06-27 14:29:13 +0200503 nlohmann::json oldConfiguration = systemConfiguration;
504 auto missingConfigurations = std::make_shared<nlohmann::json>();
505 *missingConfigurations = systemConfiguration;
James Feist899e17f2019-09-13 11:46:29 -0700506
Alexander Hansenb1340da2025-06-27 14:29:13 +0200507 auto perfScan = std::make_shared<scan::PerformScan>(
Christopher Meisf7252572025-06-11 13:22:05 +0200508 *this, *missingConfigurations, configuration.configurations, io,
Alexander Hansenb1340da2025-06-27 14:29:13 +0200509 [this, count, oldConfiguration, missingConfigurations]() {
510 // this is something that since ac has been applied to the
511 // bmc we saw, and we no longer see it
Alexander Hansen0ab32b32025-06-27 14:50:33 +0200512 bool powerOff = !powerStatus.isPowerOn();
Alexander Hansenb1340da2025-06-27 14:29:13 +0200513 for (const auto& [name, device] :
514 missingConfigurations->items())
515 {
516 pruneConfiguration(powerOff, name, device);
517 }
Alexander Hansenb1340da2025-06-27 14:29:13 +0200518 nlohmann::json newConfiguration = systemConfiguration;
519
Christopher Meisf7252572025-06-11 13:22:05 +0200520 deriveNewConfiguration(oldConfiguration, newConfiguration);
Alexander Hansenb1340da2025-06-27 14:29:13 +0200521
522 for (const auto& [_, device] : newConfiguration.items())
523 {
524 logDeviceAdded(device);
525 }
526
527 propertiesChangedInProgress = false;
528
529 boost::asio::post(io, [this, newConfiguration, count] {
530 publishNewConfiguration(
Alexander Hansenfc1b1e22025-06-27 14:34:11 +0200531 std::ref(propertiesChangedInstance), count,
Alexander Hansenb1340da2025-06-27 14:29:13 +0200532 std::ref(propertiesChangedTimer), newConfiguration);
533 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200534 });
Alexander Hansenb1340da2025-06-27 14:29:13 +0200535 perfScan->run();
536 });
James Feist75fdeeb2018-02-20 14:26:16 -0800537}
538
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600539// Check if InterfacesAdded payload contains an iface that needs probing.
Patrick Williamsb7077432024-08-16 15:22:21 -0400540static bool iaContainsProbeInterface(
Christopher Meisf7252572025-06-11 13:22:05 +0200541 sdbusplus::message_t& msg,
542 const std::unordered_set<std::string>& probeInterfaces)
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600543{
544 sdbusplus::message::object_path path;
545 DBusObject interfaces;
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600546 msg.read(path, interfaces);
Eldin Lee802cbf22025-08-22 14:44:04 +0800547 return std::ranges::any_of(interfaces | std::views::keys,
548 [&probeInterfaces](const auto& ifaceName) {
549 return probeInterfaces.contains(ifaceName);
550 });
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600551}
552
553// Check if InterfacesRemoved payload contains an iface that needs probing.
Patrick Williamsb7077432024-08-16 15:22:21 -0400554static bool irContainsProbeInterface(
Christopher Meisf7252572025-06-11 13:22:05 +0200555 sdbusplus::message_t& msg,
556 const std::unordered_set<std::string>& probeInterfaces)
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600557{
558 sdbusplus::message::object_path path;
Eldin Lee802cbf22025-08-22 14:44:04 +0800559 std::vector<std::string> interfaces;
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600560 msg.read(path, interfaces);
Eldin Lee802cbf22025-08-22 14:44:04 +0800561 return std::ranges::any_of(interfaces,
562 [&probeInterfaces](const auto& ifaceName) {
563 return probeInterfaces.contains(ifaceName);
564 });
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600565}
566
Alexander Hansenad28e402025-06-25 12:38:20 +0200567void EntityManager::handleCurrentConfigurationJson()
568{
569 if (em_utils::fwVersionIsSame())
570 {
Christopher Meisf7252572025-06-11 13:22:05 +0200571 if (std::filesystem::is_regular_file(currentConfiguration))
Alexander Hansenad28e402025-06-25 12:38:20 +0200572 {
573 // this file could just be deleted, but it's nice for debug
574 std::filesystem::create_directory(tempConfigDir);
575 std::filesystem::remove(lastConfiguration);
Christopher Meisf7252572025-06-11 13:22:05 +0200576 std::filesystem::copy(currentConfiguration, lastConfiguration);
577 std::filesystem::remove(currentConfiguration);
Alexander Hansenad28e402025-06-25 12:38:20 +0200578
579 std::ifstream jsonStream(lastConfiguration);
580 if (jsonStream.good())
581 {
582 auto data = nlohmann::json::parse(jsonStream, nullptr, false);
583 if (data.is_discarded())
584 {
585 std::cerr
586 << "syntax error in " << lastConfiguration << "\n";
587 }
588 else
589 {
590 lastJson = std::move(data);
591 }
592 }
593 else
594 {
595 std::cerr << "unable to open " << lastConfiguration << "\n";
596 }
597 }
598 }
599 else
600 {
601 // not an error, just logging at this level to make it in the journal
602 std::cerr << "Clearing previous configuration\n";
Christopher Meisf7252572025-06-11 13:22:05 +0200603 std::filesystem::remove(currentConfiguration);
Alexander Hansenad28e402025-06-25 12:38:20 +0200604 }
605}
606
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200607void EntityManager::registerCallback(const std::string& path)
James Feist75fdeeb2018-02-20 14:26:16 -0800608{
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200609 if (dbusMatches.contains(path))
610 {
611 return;
612 }
Nan Zhoua3315672022-09-20 19:48:14 +0000613
Alexander Hansen60803182025-06-27 14:41:28 +0200614 lg2::debug("creating PropertiesChanged match on {PATH}", "PATH", path);
615
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200616 std::function<void(sdbusplus::message_t & message)> eventHandler =
617 [&](sdbusplus::message_t&) { propertiesChangedCallback(); };
James Feistfd1264a2018-05-03 12:10:00 -0700618
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200619 sdbusplus::bus::match_t match(
620 static_cast<sdbusplus::bus_t&>(*systemBus),
621 "type='signal',member='PropertiesChanged',path='" + path + "'",
622 eventHandler);
623 dbusMatches.emplace(path, std::move(match));
624}
James Feistfd1264a2018-05-03 12:10:00 -0700625
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200626// We need a poke from DBus for static providers that create all their
627// objects prior to claiming a well-known name, and thus don't emit any
628// org.freedesktop.DBus.Properties signals. Similarly if a process exits
629// for any reason, expected or otherwise, we'll need a poke to remove
630// entities from DBus.
Christopher Meisf7252572025-06-11 13:22:05 +0200631void EntityManager::initFilters(
632 const std::unordered_set<std::string>& probeInterfaces)
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200633{
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200634 nameOwnerChangedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500635 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishopc76af0f2020-12-04 13:50:23 -0500636 sdbusplus::bus::match::rules::nameOwnerChanged(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200637 [this](sdbusplus::message_t& m) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400638 auto [name, oldOwner,
639 newOwner] = m.unpack<std::string, std::string, std::string>();
Patrick Williams7b8786f2022-10-10 10:23:37 -0500640
Patrick Williamsb7077432024-08-16 15:22:21 -0400641 if (name.starts_with(':'))
642 {
643 // We should do nothing with unique-name connections.
644 return;
645 }
Patrick Williams7b8786f2022-10-10 10:23:37 -0500646
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200647 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400648 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200649
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500650 // We also need a poke from DBus when new interfaces are created or
651 // destroyed.
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200652 interfacesAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500653 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500654 sdbusplus::bus::match::rules::interfacesAdded(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200655 [this, probeInterfaces](sdbusplus::message_t& msg) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400656 if (iaContainsProbeInterface(msg, probeInterfaces))
657 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200658 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400659 }
660 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200661
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200662 interfacesRemovedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500663 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500664 sdbusplus::bus::match::rules::interfacesRemoved(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200665 [this, probeInterfaces](sdbusplus::message_t& msg) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400666 if (irContainsProbeInterface(msg, probeInterfaces))
667 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200668 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400669 }
670 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200671}