blob: d17a6bb652f257bcf6762a40e1a85f198bcdb493 [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 {
Matt Spinler3d1909a2023-08-10 16:39:44 -050087 std::string boardName = boardConfig["Name"];
88 std::string boardNameOrig = boardConfig["Name"];
Andrew Jeffery13132df2022-03-25 13:29:41 +103089 std::string jsonPointerPath = "/" + boardId;
James Feist97a63f12018-05-17 13:50:57 -070090 // loop through newConfiguration, but use values from system
91 // configuration to be able to modify via dbus later
Andrew Jeffery13132df2022-03-25 13:29:41 +103092 auto boardValues = systemConfiguration[boardId];
James Feistd63d18a2018-07-19 15:23:45 -070093 auto findBoardType = boardValues.find("Type");
James Feist1b2e2242018-01-30 13:45:19 -080094 std::string boardType;
95 if (findBoardType != boardValues.end() &&
96 findBoardType->type() == nlohmann::json::value_t::string)
97 {
98 boardType = findBoardType->get<std::string>();
99 std::regex_replace(boardType.begin(), boardType.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800100 boardType.end(), illegalDbusMemberRegex, "_");
James Feist1b2e2242018-01-30 13:45:19 -0800101 }
102 else
103 {
Matt Spinler3d1909a2023-08-10 16:39:44 -0500104 std::cerr << "Unable to find type for " << boardName
James Feist1b2e2242018-01-30 13:45:19 -0800105 << " reverting to Chassis.\n";
106 boardType = "Chassis";
107 }
James Feist11be6672018-04-06 14:05:32 -0700108 std::string boardtypeLower = boost::algorithm::to_lower_copy(boardType);
James Feist1b2e2242018-01-30 13:45:19 -0800109
Matt Spinler3d1909a2023-08-10 16:39:44 -0500110 std::regex_replace(boardName.begin(), boardName.begin(),
111 boardName.end(), illegalDbusMemberRegex, "_");
112 std::string boardPath = "/xyz/openbmc_project/inventory/system/";
113 boardPath += boardtypeLower;
114 boardPath += "/";
115 boardPath += boardName;
James Feist1b2e2242018-01-30 13:45:19 -0800116
James Feistd58879a82019-09-11 11:26:07 -0700117 std::shared_ptr<sdbusplus::asio::dbus_interface> inventoryIface =
Alexander Hansen57604ed2025-06-27 13:22:28 +0200118 dbus_interface.createInterface(objServer, boardPath,
119 "xyz.openbmc_project.Inventory.Item",
120 boardName);
James Feist68500ff2018-08-08 15:40:42 -0700121
James Feistd58879a82019-09-11 11:26:07 -0700122 std::shared_ptr<sdbusplus::asio::dbus_interface> boardIface =
Alexander Hansen57604ed2025-06-27 13:22:28 +0200123 dbus_interface.createInterface(
Christopher Meis12bea9b2025-04-03 10:14:42 +0200124 objServer, boardPath,
125 "xyz.openbmc_project.Inventory.Item." + boardType,
126 boardNameOrig);
James Feist11be6672018-04-06 14:05:32 -0700127
Alexander Hansen57604ed2025-06-27 13:22:28 +0200128 dbus_interface.createAddObjectMethod(
Alexander Hansena555acf2025-06-27 11:59:10 +0200129 io, jsonPointerPath, boardPath, systemConfiguration, objServer,
Christopher Meis12bea9b2025-04-03 10:14:42 +0200130 boardNameOrig);
James Feist68500ff2018-08-08 15:40:42 -0700131
Christopher Meis12bea9b2025-04-03 10:14:42 +0200132 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200133 io, systemConfiguration, jsonPointerPath, boardIface, boardValues,
Christopher Meis12bea9b2025-04-03 10:14:42 +0200134 objServer);
James Feist97a63f12018-05-17 13:50:57 -0700135 jsonPointerPath += "/";
136 // iterate through board properties
Patrick Williams2594d362022-09-28 06:46:24 -0500137 for (const auto& [propName, propValue] : boardValues.items())
James Feist11be6672018-04-06 14:05:32 -0700138 {
Andrew Jefferya96950d2022-03-25 13:32:46 +1030139 if (propValue.type() == nlohmann::json::value_t::object)
James Feist11be6672018-04-06 14:05:32 -0700140 {
James Feistd58879a82019-09-11 11:26:07 -0700141 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
Alexander Hansen57604ed2025-06-27 13:22:28 +0200142 dbus_interface.createInterface(objServer, boardPath,
143 propName, boardNameOrig);
James Feistd58879a82019-09-11 11:26:07 -0700144
Christopher Meis12bea9b2025-04-03 10:14:42 +0200145 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200146 io, systemConfiguration, jsonPointerPath + propName, iface,
Christopher Meis12bea9b2025-04-03 10:14:42 +0200147 propValue, objServer);
James Feist11be6672018-04-06 14:05:32 -0700148 }
149 }
James Feist97a63f12018-05-17 13:50:57 -0700150
James Feist1e3e6982018-08-03 16:09:28 -0700151 auto exposes = boardValues.find("Exposes");
James Feist1b2e2242018-01-30 13:45:19 -0800152 if (exposes == boardValues.end())
153 {
154 continue;
155 }
James Feist97a63f12018-05-17 13:50:57 -0700156 // iterate through exposes
James Feist1e3e6982018-08-03 16:09:28 -0700157 jsonPointerPath += "Exposes/";
James Feist97a63f12018-05-17 13:50:57 -0700158
159 // store the board level pointer so we can modify it on the way down
160 std::string jsonPointerPathBoard = jsonPointerPath;
161 size_t exposesIndex = -1;
James Feista465ccc2019-02-08 12:51:01 -0800162 for (auto& item : *exposes)
James Feist1b2e2242018-01-30 13:45:19 -0800163 {
James Feist97a63f12018-05-17 13:50:57 -0700164 exposesIndex++;
165 jsonPointerPath = jsonPointerPathBoard;
166 jsonPointerPath += std::to_string(exposesIndex);
167
James Feistd63d18a2018-07-19 15:23:45 -0700168 auto findName = item.find("Name");
James Feist1b2e2242018-01-30 13:45:19 -0800169 if (findName == item.end())
170 {
171 std::cerr << "cannot find name in field " << item << "\n";
172 continue;
173 }
James Feist1e3e6982018-08-03 16:09:28 -0700174 auto findStatus = item.find("Status");
James Feist1b2e2242018-01-30 13:45:19 -0800175 // if status is not found it is assumed to be status = 'okay'
176 if (findStatus != item.end())
177 {
178 if (*findStatus == "disabled")
179 {
180 continue;
181 }
182 }
James Feistd63d18a2018-07-19 15:23:45 -0700183 auto findType = item.find("Type");
James Feist1b2e2242018-01-30 13:45:19 -0800184 std::string itemType;
185 if (findType != item.end())
186 {
187 itemType = findType->get<std::string>();
188 std::regex_replace(itemType.begin(), itemType.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800189 itemType.end(), illegalDbusPathRegex, "_");
James Feist1b2e2242018-01-30 13:45:19 -0800190 }
191 else
192 {
193 itemType = "unknown";
194 }
195 std::string itemName = findName->get<std::string>();
196 std::regex_replace(itemName.begin(), itemName.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800197 itemName.end(), illegalDbusMemberRegex, "_");
Matt Spinler3d1909a2023-08-10 16:39:44 -0500198 std::string ifacePath = boardPath;
Ed Tanous07d467b2021-02-23 14:48:37 -0800199 ifacePath += "/";
200 ifacePath += itemName;
James Feistc6248a52018-08-14 10:09:45 -0700201
Sui Chen74ebe592022-09-13 10:22:03 -0700202 if (itemType == "BMC")
203 {
204 std::shared_ptr<sdbusplus::asio::dbus_interface> bmcIface =
Alexander Hansen57604ed2025-06-27 13:22:28 +0200205 dbus_interface.createInterface(
Christopher Meis12bea9b2025-04-03 10:14:42 +0200206 objServer, ifacePath,
207 "xyz.openbmc_project.Inventory.Item.Bmc",
208 boardNameOrig);
209 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200210 io, systemConfiguration, jsonPointerPath, bmcIface, item,
Christopher Meis12bea9b2025-04-03 10:14:42 +0200211 objServer, getPermission(itemType));
Sui Chen74ebe592022-09-13 10:22:03 -0700212 }
Edward Leeeb587b42023-03-08 18:59:04 +0000213 else if (itemType == "System")
214 {
215 std::shared_ptr<sdbusplus::asio::dbus_interface> systemIface =
Alexander Hansen57604ed2025-06-27 13:22:28 +0200216 dbus_interface.createInterface(
Christopher Meis12bea9b2025-04-03 10:14:42 +0200217 objServer, ifacePath,
218 "xyz.openbmc_project.Inventory.Item.System",
219 boardNameOrig);
220 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200221 io, systemConfiguration, jsonPointerPath, systemIface, item,
Christopher Meis12bea9b2025-04-03 10:14:42 +0200222 objServer, getPermission(itemType));
Edward Leeeb587b42023-03-08 18:59:04 +0000223 }
Sui Chen74ebe592022-09-13 10:22:03 -0700224
Patrick Williams2594d362022-09-28 06:46:24 -0500225 for (const auto& [name, config] : item.items())
James Feist1b2e2242018-01-30 13:45:19 -0800226 {
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030227 jsonPointerPath = jsonPointerPathBoard;
228 jsonPointerPath.append(std::to_string(exposesIndex))
229 .append("/")
230 .append(name);
231 if (config.type() == nlohmann::json::value_t::object)
James Feist1b2e2242018-01-30 13:45:19 -0800232 {
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030233 std::string ifaceName =
234 "xyz.openbmc_project.Configuration.";
235 ifaceName.append(itemType).append(".").append(name);
James Feist97a63f12018-05-17 13:50:57 -0700236
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030237 std::shared_ptr<sdbusplus::asio::dbus_interface>
Alexander Hansen57604ed2025-06-27 13:22:28 +0200238 objectIface = dbus_interface.createInterface(
Christopher Meis12bea9b2025-04-03 10:14:42 +0200239 objServer, ifacePath, ifaceName, boardNameOrig);
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030240
Christopher Meis12bea9b2025-04-03 10:14:42 +0200241 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200242 io, systemConfiguration, jsonPointerPath, objectIface,
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030243 config, objServer, getPermission(name));
James Feist1b2e2242018-01-30 13:45:19 -0800244 }
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030245 else if (config.type() == nlohmann::json::value_t::array)
James Feist1b2e2242018-01-30 13:45:19 -0800246 {
247 size_t index = 0;
Ed Tanous3013fb42022-07-09 08:27:06 -0700248 if (config.empty())
James Feist1b2e2242018-01-30 13:45:19 -0800249 {
James Feist8f2710a2018-05-09 17:18:55 -0700250 continue;
251 }
252 bool isLegal = true;
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030253 auto type = config[0].type();
James Feist8f2710a2018-05-09 17:18:55 -0700254 if (type != nlohmann::json::value_t::object)
255 {
256 continue;
257 }
258
259 // verify legal json
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030260 for (const auto& arrayItem : config)
James Feist8f2710a2018-05-09 17:18:55 -0700261 {
262 if (arrayItem.type() != type)
James Feist1b2e2242018-01-30 13:45:19 -0800263 {
James Feist8f2710a2018-05-09 17:18:55 -0700264 isLegal = false;
James Feist1b2e2242018-01-30 13:45:19 -0800265 break;
266 }
James Feist8f2710a2018-05-09 17:18:55 -0700267 }
268 if (!isLegal)
269 {
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030270 std::cerr << "dbus format error" << config << "\n";
James Feist8f2710a2018-05-09 17:18:55 -0700271 break;
272 }
273
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030274 for (auto& arrayItem : config)
James Feist8f2710a2018-05-09 17:18:55 -0700275 {
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030276 std::string ifaceName =
277 "xyz.openbmc_project.Configuration.";
278 ifaceName.append(itemType).append(".").append(name);
279 ifaceName.append(std::to_string(index));
James Feist97a63f12018-05-17 13:50:57 -0700280
James Feistd58879a82019-09-11 11:26:07 -0700281 std::shared_ptr<sdbusplus::asio::dbus_interface>
Alexander Hansen57604ed2025-06-27 13:22:28 +0200282 objectIface = dbus_interface.createInterface(
Matt Spinler3d1909a2023-08-10 16:39:44 -0500283 objServer, ifacePath, ifaceName, boardNameOrig);
James Feistd58879a82019-09-11 11:26:07 -0700284
Christopher Meis12bea9b2025-04-03 10:14:42 +0200285 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200286 io, systemConfiguration,
James Feistc6248a52018-08-14 10:09:45 -0700287 jsonPointerPath + "/" + std::to_string(index),
288 objectIface, arrayItem, objServer,
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030289 getPermission(name));
James Feistbb43d022018-06-12 15:44:33 -0700290 index++;
James Feist1b2e2242018-01-30 13:45:19 -0800291 }
292 }
293 }
Benjamin Fairca2eb042022-09-13 06:40:42 +0000294
George Liu5c1a61a2024-10-24 18:02:29 +0800295 std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface =
Alexander Hansen57604ed2025-06-27 13:22:28 +0200296 dbus_interface.createInterface(
Christopher Meis12bea9b2025-04-03 10:14:42 +0200297 objServer, ifacePath,
298 "xyz.openbmc_project.Configuration." + itemType,
299 boardNameOrig);
George Liu5c1a61a2024-10-24 18:02:29 +0800300
Christopher Meis12bea9b2025-04-03 10:14:42 +0200301 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200302 io, systemConfiguration, jsonPointerPath, itemIface, item,
Christopher Meis12bea9b2025-04-03 10:14:42 +0200303 objServer, getPermission(itemType));
George Liu5c1a61a2024-10-24 18:02:29 +0800304
Matt Spinler6eb60972023-08-14 16:36:20 -0500305 topology.addBoard(boardPath, boardType, boardNameOrig, item);
James Feist1b2e2242018-01-30 13:45:19 -0800306 }
Matt Spinler6eb60972023-08-14 16:36:20 -0500307
308 newBoards.emplace(boardPath, boardNameOrig);
James Feist1b2e2242018-01-30 13:45:19 -0800309 }
Benjamin Fairca2eb042022-09-13 06:40:42 +0000310
Matt Spinler6eb60972023-08-14 16:36:20 -0500311 for (const auto& [assocPath, assocPropValue] :
312 topology.getAssocs(newBoards))
Benjamin Fairca2eb042022-09-13 06:40:42 +0000313 {
Matt Spinler6eb60972023-08-14 16:36:20 -0500314 auto findBoard = newBoards.find(assocPath);
315 if (findBoard == newBoards.end())
316 {
317 continue;
318 }
Benjamin Fairca2eb042022-09-13 06:40:42 +0000319
Alexander Hansen57604ed2025-06-27 13:22:28 +0200320 auto ifacePtr = dbus_interface.createInterface(
Matt Spinler6eb60972023-08-14 16:36:20 -0500321 objServer, assocPath, "xyz.openbmc_project.Association.Definitions",
322 findBoard->second);
323
324 ifacePtr->register_property("Associations", assocPropValue);
Christopher Meis12bea9b2025-04-03 10:14:42 +0200325 dbus_interface::tryIfaceInitialize(ifacePtr);
Benjamin Fairca2eb042022-09-13 06:40:42 +0000326 }
James Feist1b2e2242018-01-30 13:45:19 -0800327}
328
Andrew Jeffery55192932022-03-24 12:29:27 +1030329static bool deviceRequiresPowerOn(const nlohmann::json& entity)
330{
331 auto powerState = entity.find("PowerState");
Andrew Jefferyb6209442022-03-24 12:36:20 +1030332 if (powerState == entity.end())
Andrew Jeffery55192932022-03-24 12:29:27 +1030333 {
Andrew Jefferyb6209442022-03-24 12:36:20 +1030334 return false;
Andrew Jeffery55192932022-03-24 12:29:27 +1030335 }
336
Ed Tanous3013fb42022-07-09 08:27:06 -0700337 const auto* ptr = powerState->get_ptr<const std::string*>();
338 if (ptr == nullptr)
Andrew Jefferyb6209442022-03-24 12:36:20 +1030339 {
340 return false;
341 }
342
343 return *ptr == "On" || *ptr == "BiosPost";
Andrew Jeffery55192932022-03-24 12:29:27 +1030344}
345
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030346static void pruneDevice(const nlohmann::json& systemConfiguration,
347 const bool powerOff, const bool scannedPowerOff,
348 const std::string& name, const nlohmann::json& device)
349{
350 if (systemConfiguration.contains(name))
351 {
352 return;
353 }
354
Andrew Jeffery4db38bc2022-03-24 13:42:41 +1030355 if (deviceRequiresPowerOn(device) && (powerOff || scannedPowerOff))
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030356 {
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030357 return;
358 }
359
360 logDeviceRemoved(device);
361}
362
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200363void EntityManager::startRemovedTimer(boost::asio::steady_timer& timer,
364 nlohmann::json& systemConfiguration)
James Feist1df06a42019-04-11 14:23:04 -0700365{
James Feistfb00f392019-06-25 14:16:48 -0700366 if (systemConfiguration.empty() || lastJson.empty())
367 {
368 return; // not ready yet
369 }
James Feist1df06a42019-04-11 14:23:04 -0700370 if (scannedPowerOn)
371 {
372 return;
373 }
374
Alexander Hansen0ab32b32025-06-27 14:50:33 +0200375 if (!powerStatus.isPowerOn() && scannedPowerOff)
James Feist1df06a42019-04-11 14:23:04 -0700376 {
377 return;
378 }
379
James Feistb1728ca2020-04-30 15:40:55 -0700380 timer.expires_after(std::chrono::seconds(10));
Andrew Jeffery27a1cfb2022-03-24 12:31:53 +1030381 timer.async_wait(
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200382 [&systemConfiguration, this](const boost::system::error_code& ec) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400383 if (ec == boost::asio::error::operation_aborted)
384 {
385 return;
386 }
Andrew Jeffery27a1cfb2022-03-24 12:31:53 +1030387
Alexander Hansen0ab32b32025-06-27 14:50:33 +0200388 bool powerOff = !powerStatus.isPowerOn();
Patrick Williamsb7077432024-08-16 15:22:21 -0400389 for (const auto& [name, device] : lastJson.items())
390 {
391 pruneDevice(systemConfiguration, powerOff, scannedPowerOff,
392 name, device);
393 }
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030394
Patrick Williamsb7077432024-08-16 15:22:21 -0400395 scannedPowerOff = true;
396 if (!powerOff)
397 {
398 scannedPowerOn = true;
399 }
400 });
James Feist1df06a42019-04-11 14:23:04 -0700401}
402
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200403void EntityManager::pruneConfiguration(bool powerOff, const std::string& name,
404 const nlohmann::json& device)
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030405{
406 if (powerOff && deviceRequiresPowerOn(device))
407 {
408 // power not on yet, don't know if it's there or not
409 return;
410 }
411
Alexander Hansen57604ed2025-06-27 13:22:28 +0200412 auto& ifaces = dbus_interface.getDeviceInterfaces(device);
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030413 for (auto& iface : ifaces)
414 {
415 auto sharedPtr = iface.lock();
416 if (!!sharedPtr)
417 {
418 objServer.remove_interface(sharedPtr);
419 }
420 }
421
422 ifaces.clear();
423 systemConfiguration.erase(name);
Matt Spinler6eb60972023-08-14 16:36:20 -0500424 topology.remove(device["Name"].get<std::string>());
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030425 logDeviceRemoved(device);
426}
427
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200428void EntityManager::publishNewConfiguration(
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030429 const size_t& instance, const size_t count,
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200430 boost::asio::steady_timer& timer, // Gerrit discussion:
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030431 // https://gerrit.openbmc-project.xyz/c/openbmc/entity-manager/+/52316/6
432 //
433 // Discord discussion:
434 // https://discord.com/channels/775381525260664832/867820390406422538/958048437729910854
435 //
436 // NOLINTNEXTLINE(performance-unnecessary-value-param)
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200437 const nlohmann::json newConfiguration)
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030438{
Alexander Hansena555acf2025-06-27 11:59:10 +0200439 loadOverlays(newConfiguration, io);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030440
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200441 boost::asio::post(io, [this]() {
Christopher Meisf7252572025-06-11 13:22:05 +0200442 if (!writeJsonFiles(systemConfiguration))
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030443 {
444 std::cerr << "Error writing json files\n";
445 }
446 });
447
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200448 boost::asio::post(io, [this, &instance, count, &timer, newConfiguration]() {
449 postToDbus(newConfiguration);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030450 if (count == instance)
451 {
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200452 startRemovedTimer(timer, systemConfiguration);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030453 }
454 });
455}
456
James Feist8f2710a2018-05-09 17:18:55 -0700457// main properties changed entry
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200458void EntityManager::propertiesChangedCallback()
James Feist8f2710a2018-05-09 17:18:55 -0700459{
Alexander Hansenfc1b1e22025-06-27 14:34:11 +0200460 propertiesChangedInstance++;
461 size_t count = propertiesChangedInstance;
James Feist1df06a42019-04-11 14:23:04 -0700462
Alexander Hansenb1340da2025-06-27 14:29:13 +0200463 propertiesChangedTimer.expires_after(std::chrono::milliseconds(500));
James Feist8f2710a2018-05-09 17:18:55 -0700464
465 // setup an async wait as we normally get flooded with new requests
Alexander Hansenb1340da2025-06-27 14:29:13 +0200466 propertiesChangedTimer.async_wait(
467 [this, count](const boost::system::error_code& ec) {
468 if (ec == boost::asio::error::operation_aborted)
469 {
470 // we were cancelled
471 return;
472 }
473 if (ec)
474 {
475 std::cerr << "async wait error " << ec << "\n";
476 return;
477 }
James Feist8f2710a2018-05-09 17:18:55 -0700478
Alexander Hansenb1340da2025-06-27 14:29:13 +0200479 if (propertiesChangedInProgress)
480 {
481 propertiesChangedCallback();
482 return;
483 }
484 propertiesChangedInProgress = true;
James Feist2539ccd2020-05-01 16:15:08 -0700485
Alexander Hansenb1340da2025-06-27 14:29:13 +0200486 nlohmann::json oldConfiguration = systemConfiguration;
487 auto missingConfigurations = std::make_shared<nlohmann::json>();
488 *missingConfigurations = systemConfiguration;
James Feist899e17f2019-09-13 11:46:29 -0700489
Alexander Hansenb1340da2025-06-27 14:29:13 +0200490 auto perfScan = std::make_shared<scan::PerformScan>(
Christopher Meisf7252572025-06-11 13:22:05 +0200491 *this, *missingConfigurations, configuration.configurations, io,
Alexander Hansenb1340da2025-06-27 14:29:13 +0200492 [this, count, oldConfiguration, missingConfigurations]() {
493 // this is something that since ac has been applied to the
494 // bmc we saw, and we no longer see it
Alexander Hansen0ab32b32025-06-27 14:50:33 +0200495 bool powerOff = !powerStatus.isPowerOn();
Alexander Hansenb1340da2025-06-27 14:29:13 +0200496 for (const auto& [name, device] :
497 missingConfigurations->items())
498 {
499 pruneConfiguration(powerOff, name, device);
500 }
Alexander Hansenb1340da2025-06-27 14:29:13 +0200501 nlohmann::json newConfiguration = systemConfiguration;
502
Christopher Meisf7252572025-06-11 13:22:05 +0200503 deriveNewConfiguration(oldConfiguration, newConfiguration);
Alexander Hansenb1340da2025-06-27 14:29:13 +0200504
505 for (const auto& [_, device] : newConfiguration.items())
506 {
507 logDeviceAdded(device);
508 }
509
510 propertiesChangedInProgress = false;
511
512 boost::asio::post(io, [this, newConfiguration, count] {
513 publishNewConfiguration(
Alexander Hansenfc1b1e22025-06-27 14:34:11 +0200514 std::ref(propertiesChangedInstance), count,
Alexander Hansenb1340da2025-06-27 14:29:13 +0200515 std::ref(propertiesChangedTimer), newConfiguration);
516 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200517 });
Alexander Hansenb1340da2025-06-27 14:29:13 +0200518 perfScan->run();
519 });
James Feist75fdeeb2018-02-20 14:26:16 -0800520}
521
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600522// Check if InterfacesAdded payload contains an iface that needs probing.
Patrick Williamsb7077432024-08-16 15:22:21 -0400523static bool iaContainsProbeInterface(
Christopher Meisf7252572025-06-11 13:22:05 +0200524 sdbusplus::message_t& msg,
525 const std::unordered_set<std::string>& probeInterfaces)
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600526{
527 sdbusplus::message::object_path path;
528 DBusObject interfaces;
529 std::set<std::string> interfaceSet;
530 std::set<std::string> intersect;
531
532 msg.read(path, interfaces);
533
534 std::for_each(interfaces.begin(), interfaces.end(),
535 [&interfaceSet](const auto& iface) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400536 interfaceSet.insert(iface.first);
537 });
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600538
539 std::set_intersection(interfaceSet.begin(), interfaceSet.end(),
540 probeInterfaces.begin(), probeInterfaces.end(),
541 std::inserter(intersect, intersect.end()));
542 return !intersect.empty();
543}
544
545// Check if InterfacesRemoved payload contains an iface that needs probing.
Patrick Williamsb7077432024-08-16 15:22:21 -0400546static bool irContainsProbeInterface(
Christopher Meisf7252572025-06-11 13:22:05 +0200547 sdbusplus::message_t& msg,
548 const std::unordered_set<std::string>& probeInterfaces)
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600549{
550 sdbusplus::message::object_path path;
551 std::set<std::string> interfaces;
552 std::set<std::string> intersect;
553
554 msg.read(path, interfaces);
555
556 std::set_intersection(interfaces.begin(), interfaces.end(),
557 probeInterfaces.begin(), probeInterfaces.end(),
558 std::inserter(intersect, intersect.end()));
559 return !intersect.empty();
560}
561
Alexander Hansenad28e402025-06-25 12:38:20 +0200562void EntityManager::handleCurrentConfigurationJson()
563{
564 if (em_utils::fwVersionIsSame())
565 {
Christopher Meisf7252572025-06-11 13:22:05 +0200566 if (std::filesystem::is_regular_file(currentConfiguration))
Alexander Hansenad28e402025-06-25 12:38:20 +0200567 {
568 // this file could just be deleted, but it's nice for debug
569 std::filesystem::create_directory(tempConfigDir);
570 std::filesystem::remove(lastConfiguration);
Christopher Meisf7252572025-06-11 13:22:05 +0200571 std::filesystem::copy(currentConfiguration, lastConfiguration);
572 std::filesystem::remove(currentConfiguration);
Alexander Hansenad28e402025-06-25 12:38:20 +0200573
574 std::ifstream jsonStream(lastConfiguration);
575 if (jsonStream.good())
576 {
577 auto data = nlohmann::json::parse(jsonStream, nullptr, false);
578 if (data.is_discarded())
579 {
580 std::cerr
581 << "syntax error in " << lastConfiguration << "\n";
582 }
583 else
584 {
585 lastJson = std::move(data);
586 }
587 }
588 else
589 {
590 std::cerr << "unable to open " << lastConfiguration << "\n";
591 }
592 }
593 }
594 else
595 {
596 // not an error, just logging at this level to make it in the journal
597 std::cerr << "Clearing previous configuration\n";
Christopher Meisf7252572025-06-11 13:22:05 +0200598 std::filesystem::remove(currentConfiguration);
Alexander Hansenad28e402025-06-25 12:38:20 +0200599 }
600}
601
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200602void EntityManager::registerCallback(const std::string& path)
James Feist75fdeeb2018-02-20 14:26:16 -0800603{
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200604 if (dbusMatches.contains(path))
605 {
606 return;
607 }
Nan Zhoua3315672022-09-20 19:48:14 +0000608
Alexander Hansen60803182025-06-27 14:41:28 +0200609 lg2::debug("creating PropertiesChanged match on {PATH}", "PATH", path);
610
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200611 std::function<void(sdbusplus::message_t & message)> eventHandler =
612 [&](sdbusplus::message_t&) { propertiesChangedCallback(); };
James Feistfd1264a2018-05-03 12:10:00 -0700613
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200614 sdbusplus::bus::match_t match(
615 static_cast<sdbusplus::bus_t&>(*systemBus),
616 "type='signal',member='PropertiesChanged',path='" + path + "'",
617 eventHandler);
618 dbusMatches.emplace(path, std::move(match));
619}
James Feistfd1264a2018-05-03 12:10:00 -0700620
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200621// We need a poke from DBus for static providers that create all their
622// objects prior to claiming a well-known name, and thus don't emit any
623// org.freedesktop.DBus.Properties signals. Similarly if a process exits
624// for any reason, expected or otherwise, we'll need a poke to remove
625// entities from DBus.
Christopher Meisf7252572025-06-11 13:22:05 +0200626void EntityManager::initFilters(
627 const std::unordered_set<std::string>& probeInterfaces)
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200628{
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200629 nameOwnerChangedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500630 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishopc76af0f2020-12-04 13:50:23 -0500631 sdbusplus::bus::match::rules::nameOwnerChanged(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200632 [this](sdbusplus::message_t& m) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400633 auto [name, oldOwner,
634 newOwner] = m.unpack<std::string, std::string, std::string>();
Patrick Williams7b8786f2022-10-10 10:23:37 -0500635
Patrick Williamsb7077432024-08-16 15:22:21 -0400636 if (name.starts_with(':'))
637 {
638 // We should do nothing with unique-name connections.
639 return;
640 }
Patrick Williams7b8786f2022-10-10 10:23:37 -0500641
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200642 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400643 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200644
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500645 // We also need a poke from DBus when new interfaces are created or
646 // destroyed.
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200647 interfacesAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500648 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500649 sdbusplus::bus::match::rules::interfacesAdded(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200650 [this, probeInterfaces](sdbusplus::message_t& msg) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400651 if (iaContainsProbeInterface(msg, probeInterfaces))
652 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200653 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400654 }
655 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200656
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200657 interfacesRemovedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500658 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500659 sdbusplus::bus::match::rules::interfacesRemoved(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200660 [this, probeInterfaces](sdbusplus::message_t& msg) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400661 if (irContainsProbeInterface(msg, probeInterfaces))
662 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200663 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400664 }
665 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200666}