blob: 31e7ef4f44b9ad732f0a91813af04dfd6a283133 [file] [log] [blame]
James Feist3cb5fec2018-01-23 14:41:51 -08001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
Brad Bishope45d8c72022-05-25 15:12:53 -040016/// \file entity_manager.cpp
James Feist3cb5fec2018-01-23 14:41:51 -080017
Brad Bishope45d8c72022-05-25 15:12:53 -040018#include "entity_manager.hpp"
James Feist1df06a42019-04-11 14:23:04 -070019
Christopher Meisfc9e7fd2025-04-03 13:13:35 +020020#include "../utils.hpp"
21#include "../variant_visitors.hpp"
Christopher Meisbdaa6b22025-04-02 10:49:02 +020022#include "configuration.hpp"
Christopher Meis12bea9b2025-04-03 10:14:42 +020023#include "dbus_interface.hpp"
Brad Bishope45d8c72022-05-25 15:12:53 -040024#include "overlay.hpp"
Christopher Meis26fbbd52025-03-26 14:55:06 +010025#include "perform_scan.hpp"
Alexander Hansen60803182025-06-27 14:41:28 +020026#include "phosphor-logging/lg2.hpp"
Benjamin Fairca2eb042022-09-13 06:40:42 +000027#include "topology.hpp"
Christopher Meis59ef1e72025-04-16 08:53:25 +020028#include "utils.hpp"
James Feist481c5d52019-08-13 14:40:40 -070029
James Feist11be6672018-04-06 14:05:32 -070030#include <boost/algorithm/string/case_conv.hpp>
James Feistf5125b02019-06-06 11:27:43 -070031#include <boost/algorithm/string/classification.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080032#include <boost/algorithm/string/predicate.hpp>
33#include <boost/algorithm/string/replace.hpp>
James Feistf5125b02019-06-06 11:27:43 -070034#include <boost/algorithm/string/split.hpp>
James Feist02d2b932020-02-06 16:28:48 -080035#include <boost/asio/io_context.hpp>
Ed Tanous49a888c2023-03-06 13:44:51 -080036#include <boost/asio/post.hpp>
James Feistb1728ca2020-04-30 15:40:55 -070037#include <boost/asio/steady_timer.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080038#include <boost/container/flat_map.hpp>
39#include <boost/container/flat_set.hpp>
James Feistf5125b02019-06-06 11:27:43 -070040#include <boost/range/iterator_range.hpp>
James Feist8c505da2020-05-28 10:06:33 -070041#include <nlohmann/json.hpp>
42#include <sdbusplus/asio/connection.hpp>
43#include <sdbusplus/asio/object_server.hpp>
44
James Feist637b3ef2019-04-15 16:35:30 -070045#include <filesystem>
James Feista465ccc2019-02-08 12:51:01 -080046#include <fstream>
Andrew Jefferye35d0ac2022-03-24 15:53:13 +103047#include <functional>
James Feista465ccc2019-02-08 12:51:01 -080048#include <iostream>
Andrew Jeffery666583b2021-12-01 15:50:12 +103049#include <map>
James Feista465ccc2019-02-08 12:51:01 -080050#include <regex>
James Feist1df06a42019-04-11 14:23:04 -070051constexpr const char* tempConfigDir = "/tmp/configuration/";
52constexpr const char* lastConfiguration = "/tmp/configuration/last.json";
James Feistf1b14142019-04-10 15:22:09 -070053
Adrian Ambrożewiczc789fca2020-05-14 15:50:05 +020054static constexpr std::array<const char*, 6> settableInterfaces = {
55 "FanProfile", "Pid", "Pid.Zone", "Stepwise", "Thresholds", "Polling"};
James Feist3cb5fec2018-01-23 14:41:51 -080056
Ed Tanous07d467b2021-02-23 14:48:37 -080057const std::regex illegalDbusPathRegex("[^A-Za-z0-9_.]");
58const std::regex illegalDbusMemberRegex("[^A-Za-z0-9_]");
James Feist1b2e2242018-01-30 13:45:19 -080059
James Feista465ccc2019-02-08 12:51:01 -080060sdbusplus::asio::PropertyPermission getPermission(const std::string& interface)
James Feistc6248a52018-08-14 10:09:45 -070061{
62 return std::find(settableInterfaces.begin(), settableInterfaces.end(),
63 interface) != settableInterfaces.end()
64 ? sdbusplus::asio::PropertyPermission::readWrite
65 : sdbusplus::asio::PropertyPermission::readOnly;
66}
67
Christopher Meiscf6a75b2025-06-03 07:53:50 +020068EntityManager::EntityManager(
Alexander Hansena555acf2025-06-27 11:59:10 +020069 std::shared_ptr<sdbusplus::asio::connection>& systemBus,
70 boost::asio::io_context& io) :
Christopher Meiscf6a75b2025-06-03 07:53:50 +020071 systemBus(systemBus),
72 objServer(sdbusplus::asio::object_server(systemBus, /*skipManager=*/true)),
73 lastJson(nlohmann::json::object()),
Alexander Hansenb1340da2025-06-27 14:29:13 +020074 systemConfiguration(nlohmann::json::object()), io(io),
75 propertiesChangedTimer(io)
Christopher Meiscf6a75b2025-06-03 07:53:50 +020076{
77 // All other objects that EntityManager currently support are under the
78 // inventory subtree.
79 // See the discussion at
80 // https://discord.com/channels/775381525260664832/1018929092009144380
81 objServer.add_manager("/xyz/openbmc_project/inventory");
James Feist75fdeeb2018-02-20 14:26:16 -080082
Christopher Meiscf6a75b2025-06-03 07:53:50 +020083 entityIface = objServer.add_interface("/xyz/openbmc_project/EntityManager",
84 "xyz.openbmc_project.EntityManager");
85 entityIface->register_method("ReScan", [this]() {
86 propertiesChangedCallback();
87 });
88 dbus_interface::tryIfaceInitialize(entityIface);
89}
90
91void EntityManager::postToDbus(const nlohmann::json& newConfiguration)
James Feist1b2e2242018-01-30 13:45:19 -080092{
Matt Spinler6eb60972023-08-14 16:36:20 -050093 std::map<std::string, std::string> newBoards; // path -> name
Benjamin Fairca2eb042022-09-13 06:40:42 +000094
James Feist97a63f12018-05-17 13:50:57 -070095 // iterate through boards
Patrick Williams2594d362022-09-28 06:46:24 -050096 for (const auto& [boardId, boardConfig] : newConfiguration.items())
James Feist1b2e2242018-01-30 13:45:19 -080097 {
Matt Spinler3d1909a2023-08-10 16:39:44 -050098 std::string boardName = boardConfig["Name"];
99 std::string boardNameOrig = boardConfig["Name"];
Andrew Jeffery13132df2022-03-25 13:29:41 +1030100 std::string jsonPointerPath = "/" + boardId;
James Feist97a63f12018-05-17 13:50:57 -0700101 // loop through newConfiguration, but use values from system
102 // configuration to be able to modify via dbus later
Andrew Jeffery13132df2022-03-25 13:29:41 +1030103 auto boardValues = systemConfiguration[boardId];
James Feistd63d18a2018-07-19 15:23:45 -0700104 auto findBoardType = boardValues.find("Type");
James Feist1b2e2242018-01-30 13:45:19 -0800105 std::string boardType;
106 if (findBoardType != boardValues.end() &&
107 findBoardType->type() == nlohmann::json::value_t::string)
108 {
109 boardType = findBoardType->get<std::string>();
110 std::regex_replace(boardType.begin(), boardType.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800111 boardType.end(), illegalDbusMemberRegex, "_");
James Feist1b2e2242018-01-30 13:45:19 -0800112 }
113 else
114 {
Matt Spinler3d1909a2023-08-10 16:39:44 -0500115 std::cerr << "Unable to find type for " << boardName
James Feist1b2e2242018-01-30 13:45:19 -0800116 << " reverting to Chassis.\n";
117 boardType = "Chassis";
118 }
James Feist11be6672018-04-06 14:05:32 -0700119 std::string boardtypeLower = boost::algorithm::to_lower_copy(boardType);
James Feist1b2e2242018-01-30 13:45:19 -0800120
Matt Spinler3d1909a2023-08-10 16:39:44 -0500121 std::regex_replace(boardName.begin(), boardName.begin(),
122 boardName.end(), illegalDbusMemberRegex, "_");
123 std::string boardPath = "/xyz/openbmc_project/inventory/system/";
124 boardPath += boardtypeLower;
125 boardPath += "/";
126 boardPath += boardName;
James Feist1b2e2242018-01-30 13:45:19 -0800127
James Feistd58879a82019-09-11 11:26:07 -0700128 std::shared_ptr<sdbusplus::asio::dbus_interface> inventoryIface =
Alexander Hansen57604ed2025-06-27 13:22:28 +0200129 dbus_interface.createInterface(objServer, boardPath,
130 "xyz.openbmc_project.Inventory.Item",
131 boardName);
James Feist68500ff2018-08-08 15:40:42 -0700132
James Feistd58879a82019-09-11 11:26:07 -0700133 std::shared_ptr<sdbusplus::asio::dbus_interface> boardIface =
Alexander Hansen57604ed2025-06-27 13:22:28 +0200134 dbus_interface.createInterface(
Christopher Meis12bea9b2025-04-03 10:14:42 +0200135 objServer, boardPath,
136 "xyz.openbmc_project.Inventory.Item." + boardType,
137 boardNameOrig);
James Feist11be6672018-04-06 14:05:32 -0700138
Alexander Hansen57604ed2025-06-27 13:22:28 +0200139 dbus_interface.createAddObjectMethod(
Alexander Hansena555acf2025-06-27 11:59:10 +0200140 io, jsonPointerPath, boardPath, systemConfiguration, objServer,
Christopher Meis12bea9b2025-04-03 10:14:42 +0200141 boardNameOrig);
James Feist68500ff2018-08-08 15:40:42 -0700142
Christopher Meis12bea9b2025-04-03 10:14:42 +0200143 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200144 io, systemConfiguration, jsonPointerPath, boardIface, boardValues,
Christopher Meis12bea9b2025-04-03 10:14:42 +0200145 objServer);
James Feist97a63f12018-05-17 13:50:57 -0700146 jsonPointerPath += "/";
147 // iterate through board properties
Patrick Williams2594d362022-09-28 06:46:24 -0500148 for (const auto& [propName, propValue] : boardValues.items())
James Feist11be6672018-04-06 14:05:32 -0700149 {
Andrew Jefferya96950d2022-03-25 13:32:46 +1030150 if (propValue.type() == nlohmann::json::value_t::object)
James Feist11be6672018-04-06 14:05:32 -0700151 {
James Feistd58879a82019-09-11 11:26:07 -0700152 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
Alexander Hansen57604ed2025-06-27 13:22:28 +0200153 dbus_interface.createInterface(objServer, boardPath,
154 propName, boardNameOrig);
James Feistd58879a82019-09-11 11:26:07 -0700155
Christopher Meis12bea9b2025-04-03 10:14:42 +0200156 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200157 io, systemConfiguration, jsonPointerPath + propName, iface,
Christopher Meis12bea9b2025-04-03 10:14:42 +0200158 propValue, objServer);
James Feist11be6672018-04-06 14:05:32 -0700159 }
160 }
James Feist97a63f12018-05-17 13:50:57 -0700161
James Feist1e3e6982018-08-03 16:09:28 -0700162 auto exposes = boardValues.find("Exposes");
James Feist1b2e2242018-01-30 13:45:19 -0800163 if (exposes == boardValues.end())
164 {
165 continue;
166 }
James Feist97a63f12018-05-17 13:50:57 -0700167 // iterate through exposes
James Feist1e3e6982018-08-03 16:09:28 -0700168 jsonPointerPath += "Exposes/";
James Feist97a63f12018-05-17 13:50:57 -0700169
170 // store the board level pointer so we can modify it on the way down
171 std::string jsonPointerPathBoard = jsonPointerPath;
172 size_t exposesIndex = -1;
James Feista465ccc2019-02-08 12:51:01 -0800173 for (auto& item : *exposes)
James Feist1b2e2242018-01-30 13:45:19 -0800174 {
James Feist97a63f12018-05-17 13:50:57 -0700175 exposesIndex++;
176 jsonPointerPath = jsonPointerPathBoard;
177 jsonPointerPath += std::to_string(exposesIndex);
178
James Feistd63d18a2018-07-19 15:23:45 -0700179 auto findName = item.find("Name");
James Feist1b2e2242018-01-30 13:45:19 -0800180 if (findName == item.end())
181 {
182 std::cerr << "cannot find name in field " << item << "\n";
183 continue;
184 }
James Feist1e3e6982018-08-03 16:09:28 -0700185 auto findStatus = item.find("Status");
James Feist1b2e2242018-01-30 13:45:19 -0800186 // if status is not found it is assumed to be status = 'okay'
187 if (findStatus != item.end())
188 {
189 if (*findStatus == "disabled")
190 {
191 continue;
192 }
193 }
James Feistd63d18a2018-07-19 15:23:45 -0700194 auto findType = item.find("Type");
James Feist1b2e2242018-01-30 13:45:19 -0800195 std::string itemType;
196 if (findType != item.end())
197 {
198 itemType = findType->get<std::string>();
199 std::regex_replace(itemType.begin(), itemType.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800200 itemType.end(), illegalDbusPathRegex, "_");
James Feist1b2e2242018-01-30 13:45:19 -0800201 }
202 else
203 {
204 itemType = "unknown";
205 }
206 std::string itemName = findName->get<std::string>();
207 std::regex_replace(itemName.begin(), itemName.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800208 itemName.end(), illegalDbusMemberRegex, "_");
Matt Spinler3d1909a2023-08-10 16:39:44 -0500209 std::string ifacePath = boardPath;
Ed Tanous07d467b2021-02-23 14:48:37 -0800210 ifacePath += "/";
211 ifacePath += itemName;
James Feistc6248a52018-08-14 10:09:45 -0700212
Sui Chen74ebe592022-09-13 10:22:03 -0700213 if (itemType == "BMC")
214 {
215 std::shared_ptr<sdbusplus::asio::dbus_interface> bmcIface =
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.Bmc",
219 boardNameOrig);
220 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200221 io, systemConfiguration, jsonPointerPath, bmcIface, item,
Christopher Meis12bea9b2025-04-03 10:14:42 +0200222 objServer, getPermission(itemType));
Sui Chen74ebe592022-09-13 10:22:03 -0700223 }
Edward Leeeb587b42023-03-08 18:59:04 +0000224 else if (itemType == "System")
225 {
226 std::shared_ptr<sdbusplus::asio::dbus_interface> systemIface =
Alexander Hansen57604ed2025-06-27 13:22:28 +0200227 dbus_interface.createInterface(
Christopher Meis12bea9b2025-04-03 10:14:42 +0200228 objServer, ifacePath,
229 "xyz.openbmc_project.Inventory.Item.System",
230 boardNameOrig);
231 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200232 io, systemConfiguration, jsonPointerPath, systemIface, item,
Christopher Meis12bea9b2025-04-03 10:14:42 +0200233 objServer, getPermission(itemType));
Edward Leeeb587b42023-03-08 18:59:04 +0000234 }
Sui Chen74ebe592022-09-13 10:22:03 -0700235
Patrick Williams2594d362022-09-28 06:46:24 -0500236 for (const auto& [name, config] : item.items())
James Feist1b2e2242018-01-30 13:45:19 -0800237 {
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030238 jsonPointerPath = jsonPointerPathBoard;
239 jsonPointerPath.append(std::to_string(exposesIndex))
240 .append("/")
241 .append(name);
242 if (config.type() == nlohmann::json::value_t::object)
James Feist1b2e2242018-01-30 13:45:19 -0800243 {
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030244 std::string ifaceName =
245 "xyz.openbmc_project.Configuration.";
246 ifaceName.append(itemType).append(".").append(name);
James Feist97a63f12018-05-17 13:50:57 -0700247
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030248 std::shared_ptr<sdbusplus::asio::dbus_interface>
Alexander Hansen57604ed2025-06-27 13:22:28 +0200249 objectIface = dbus_interface.createInterface(
Christopher Meis12bea9b2025-04-03 10:14:42 +0200250 objServer, ifacePath, ifaceName, boardNameOrig);
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030251
Christopher Meis12bea9b2025-04-03 10:14:42 +0200252 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200253 io, systemConfiguration, jsonPointerPath, objectIface,
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030254 config, objServer, getPermission(name));
James Feist1b2e2242018-01-30 13:45:19 -0800255 }
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030256 else if (config.type() == nlohmann::json::value_t::array)
James Feist1b2e2242018-01-30 13:45:19 -0800257 {
258 size_t index = 0;
Ed Tanous3013fb42022-07-09 08:27:06 -0700259 if (config.empty())
James Feist1b2e2242018-01-30 13:45:19 -0800260 {
James Feist8f2710a2018-05-09 17:18:55 -0700261 continue;
262 }
263 bool isLegal = true;
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030264 auto type = config[0].type();
James Feist8f2710a2018-05-09 17:18:55 -0700265 if (type != nlohmann::json::value_t::object)
266 {
267 continue;
268 }
269
270 // verify legal json
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030271 for (const auto& arrayItem : config)
James Feist8f2710a2018-05-09 17:18:55 -0700272 {
273 if (arrayItem.type() != type)
James Feist1b2e2242018-01-30 13:45:19 -0800274 {
James Feist8f2710a2018-05-09 17:18:55 -0700275 isLegal = false;
James Feist1b2e2242018-01-30 13:45:19 -0800276 break;
277 }
James Feist8f2710a2018-05-09 17:18:55 -0700278 }
279 if (!isLegal)
280 {
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030281 std::cerr << "dbus format error" << config << "\n";
James Feist8f2710a2018-05-09 17:18:55 -0700282 break;
283 }
284
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030285 for (auto& arrayItem : config)
James Feist8f2710a2018-05-09 17:18:55 -0700286 {
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030287 std::string ifaceName =
288 "xyz.openbmc_project.Configuration.";
289 ifaceName.append(itemType).append(".").append(name);
290 ifaceName.append(std::to_string(index));
James Feist97a63f12018-05-17 13:50:57 -0700291
James Feistd58879a82019-09-11 11:26:07 -0700292 std::shared_ptr<sdbusplus::asio::dbus_interface>
Alexander Hansen57604ed2025-06-27 13:22:28 +0200293 objectIface = dbus_interface.createInterface(
Matt Spinler3d1909a2023-08-10 16:39:44 -0500294 objServer, ifacePath, ifaceName, boardNameOrig);
James Feistd58879a82019-09-11 11:26:07 -0700295
Christopher Meis12bea9b2025-04-03 10:14:42 +0200296 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200297 io, systemConfiguration,
James Feistc6248a52018-08-14 10:09:45 -0700298 jsonPointerPath + "/" + std::to_string(index),
299 objectIface, arrayItem, objServer,
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030300 getPermission(name));
James Feistbb43d022018-06-12 15:44:33 -0700301 index++;
James Feist1b2e2242018-01-30 13:45:19 -0800302 }
303 }
304 }
Benjamin Fairca2eb042022-09-13 06:40:42 +0000305
George Liu5c1a61a2024-10-24 18:02:29 +0800306 std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface =
Alexander Hansen57604ed2025-06-27 13:22:28 +0200307 dbus_interface.createInterface(
Christopher Meis12bea9b2025-04-03 10:14:42 +0200308 objServer, ifacePath,
309 "xyz.openbmc_project.Configuration." + itemType,
310 boardNameOrig);
George Liu5c1a61a2024-10-24 18:02:29 +0800311
Christopher Meis12bea9b2025-04-03 10:14:42 +0200312 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200313 io, systemConfiguration, jsonPointerPath, itemIface, item,
Christopher Meis12bea9b2025-04-03 10:14:42 +0200314 objServer, getPermission(itemType));
George Liu5c1a61a2024-10-24 18:02:29 +0800315
Matt Spinler6eb60972023-08-14 16:36:20 -0500316 topology.addBoard(boardPath, boardType, boardNameOrig, item);
James Feist1b2e2242018-01-30 13:45:19 -0800317 }
Matt Spinler6eb60972023-08-14 16:36:20 -0500318
319 newBoards.emplace(boardPath, boardNameOrig);
James Feist1b2e2242018-01-30 13:45:19 -0800320 }
Benjamin Fairca2eb042022-09-13 06:40:42 +0000321
Matt Spinler6eb60972023-08-14 16:36:20 -0500322 for (const auto& [assocPath, assocPropValue] :
323 topology.getAssocs(newBoards))
Benjamin Fairca2eb042022-09-13 06:40:42 +0000324 {
Matt Spinler6eb60972023-08-14 16:36:20 -0500325 auto findBoard = newBoards.find(assocPath);
326 if (findBoard == newBoards.end())
327 {
328 continue;
329 }
Benjamin Fairca2eb042022-09-13 06:40:42 +0000330
Alexander Hansen57604ed2025-06-27 13:22:28 +0200331 auto ifacePtr = dbus_interface.createInterface(
Matt Spinler6eb60972023-08-14 16:36:20 -0500332 objServer, assocPath, "xyz.openbmc_project.Association.Definitions",
333 findBoard->second);
334
335 ifacePtr->register_property("Associations", assocPropValue);
Christopher Meis12bea9b2025-04-03 10:14:42 +0200336 dbus_interface::tryIfaceInitialize(ifacePtr);
Benjamin Fairca2eb042022-09-13 06:40:42 +0000337 }
James Feist1b2e2242018-01-30 13:45:19 -0800338}
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
Christopher Meis59ef1e72025-04-16 08:53:25 +0200386 if (!em_utils::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
Christopher Meis59ef1e72025-04-16 08:53:25 +0200399 bool powerOff = !em_utils::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 Meisbdaa6b22025-04-02 10:49:02 +0200453 if (!configuration::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 std::list<nlohmann::json> configurations;
502 if (!configuration::loadConfigurations(configurations))
503 {
504 std::cerr << "Could not load configurations\n";
Alexander Hansen16d40022025-06-27 14:22:56 +0200505 propertiesChangedInProgress = false;
Alexander Hansenb1340da2025-06-27 14:29:13 +0200506 return;
507 }
James Feist2539ccd2020-05-01 16:15:08 -0700508
Alexander Hansenb1340da2025-06-27 14:29:13 +0200509 auto perfScan = std::make_shared<scan::PerformScan>(
510 *this, *missingConfigurations, configurations, io,
511 [this, count, oldConfiguration, missingConfigurations]() {
512 // this is something that since ac has been applied to the
513 // bmc we saw, and we no longer see it
514 bool powerOff = !em_utils::isPowerOn();
515 for (const auto& [name, device] :
516 missingConfigurations->items())
517 {
518 pruneConfiguration(powerOff, name, device);
519 }
520
521 nlohmann::json newConfiguration = systemConfiguration;
522
523 configuration::deriveNewConfiguration(oldConfiguration,
524 newConfiguration);
525
526 for (const auto& [_, device] : newConfiguration.items())
527 {
528 logDeviceAdded(device);
529 }
530
531 propertiesChangedInProgress = false;
532
533 boost::asio::post(io, [this, newConfiguration, count] {
534 publishNewConfiguration(
Alexander Hansenfc1b1e22025-06-27 14:34:11 +0200535 std::ref(propertiesChangedInstance), count,
Alexander Hansenb1340da2025-06-27 14:29:13 +0200536 std::ref(propertiesChangedTimer), newConfiguration);
537 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200538 });
Alexander Hansenb1340da2025-06-27 14:29:13 +0200539 perfScan->run();
540 });
James Feist75fdeeb2018-02-20 14:26:16 -0800541}
542
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600543// Check if InterfacesAdded payload contains an iface that needs probing.
Patrick Williamsb7077432024-08-16 15:22:21 -0400544static bool iaContainsProbeInterface(
545 sdbusplus::message_t& msg, const std::set<std::string>& probeInterfaces)
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600546{
547 sdbusplus::message::object_path path;
548 DBusObject interfaces;
549 std::set<std::string> interfaceSet;
550 std::set<std::string> intersect;
551
552 msg.read(path, interfaces);
553
554 std::for_each(interfaces.begin(), interfaces.end(),
555 [&interfaceSet](const auto& iface) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400556 interfaceSet.insert(iface.first);
557 });
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600558
559 std::set_intersection(interfaceSet.begin(), interfaceSet.end(),
560 probeInterfaces.begin(), probeInterfaces.end(),
561 std::inserter(intersect, intersect.end()));
562 return !intersect.empty();
563}
564
565// Check if InterfacesRemoved payload contains an iface that needs probing.
Patrick Williamsb7077432024-08-16 15:22:21 -0400566static bool irContainsProbeInterface(
567 sdbusplus::message_t& msg, const std::set<std::string>& probeInterfaces)
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600568{
569 sdbusplus::message::object_path path;
570 std::set<std::string> interfaces;
571 std::set<std::string> intersect;
572
573 msg.read(path, interfaces);
574
575 std::set_intersection(interfaces.begin(), interfaces.end(),
576 probeInterfaces.begin(), probeInterfaces.end(),
577 std::inserter(intersect, intersect.end()));
578 return !intersect.empty();
579}
580
Alexander Hansenad28e402025-06-25 12:38:20 +0200581void EntityManager::handleCurrentConfigurationJson()
582{
583 if (em_utils::fwVersionIsSame())
584 {
585 if (std::filesystem::is_regular_file(
586 configuration::currentConfiguration))
587 {
588 // this file could just be deleted, but it's nice for debug
589 std::filesystem::create_directory(tempConfigDir);
590 std::filesystem::remove(lastConfiguration);
591 std::filesystem::copy(configuration::currentConfiguration,
592 lastConfiguration);
593 std::filesystem::remove(configuration::currentConfiguration);
594
595 std::ifstream jsonStream(lastConfiguration);
596 if (jsonStream.good())
597 {
598 auto data = nlohmann::json::parse(jsonStream, nullptr, false);
599 if (data.is_discarded())
600 {
601 std::cerr
602 << "syntax error in " << lastConfiguration << "\n";
603 }
604 else
605 {
606 lastJson = std::move(data);
607 }
608 }
609 else
610 {
611 std::cerr << "unable to open " << lastConfiguration << "\n";
612 }
613 }
614 }
615 else
616 {
617 // not an error, just logging at this level to make it in the journal
618 std::cerr << "Clearing previous configuration\n";
619 std::filesystem::remove(configuration::currentConfiguration);
620 }
621}
622
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200623void EntityManager::registerCallback(const std::string& path)
James Feist75fdeeb2018-02-20 14:26:16 -0800624{
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200625 if (dbusMatches.contains(path))
626 {
627 return;
628 }
Nan Zhoua3315672022-09-20 19:48:14 +0000629
Alexander Hansen60803182025-06-27 14:41:28 +0200630 lg2::debug("creating PropertiesChanged match on {PATH}", "PATH", path);
631
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200632 std::function<void(sdbusplus::message_t & message)> eventHandler =
633 [&](sdbusplus::message_t&) { propertiesChangedCallback(); };
James Feistfd1264a2018-05-03 12:10:00 -0700634
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200635 sdbusplus::bus::match_t match(
636 static_cast<sdbusplus::bus_t&>(*systemBus),
637 "type='signal',member='PropertiesChanged',path='" + path + "'",
638 eventHandler);
639 dbusMatches.emplace(path, std::move(match));
640}
James Feistfd1264a2018-05-03 12:10:00 -0700641
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200642// We need a poke from DBus for static providers that create all their
643// objects prior to claiming a well-known name, and thus don't emit any
644// org.freedesktop.DBus.Properties signals. Similarly if a process exits
645// for any reason, expected or otherwise, we'll need a poke to remove
646// entities from DBus.
647void EntityManager::initFilters(const std::set<std::string>& probeInterfaces)
648{
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200649 nameOwnerChangedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500650 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishopc76af0f2020-12-04 13:50:23 -0500651 sdbusplus::bus::match::rules::nameOwnerChanged(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200652 [this](sdbusplus::message_t& m) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400653 auto [name, oldOwner,
654 newOwner] = m.unpack<std::string, std::string, std::string>();
Patrick Williams7b8786f2022-10-10 10:23:37 -0500655
Patrick Williamsb7077432024-08-16 15:22:21 -0400656 if (name.starts_with(':'))
657 {
658 // We should do nothing with unique-name connections.
659 return;
660 }
Patrick Williams7b8786f2022-10-10 10:23:37 -0500661
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200662 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400663 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200664
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500665 // We also need a poke from DBus when new interfaces are created or
666 // destroyed.
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200667 interfacesAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500668 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500669 sdbusplus::bus::match::rules::interfacesAdded(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200670 [this, probeInterfaces](sdbusplus::message_t& msg) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400671 if (iaContainsProbeInterface(msg, probeInterfaces))
672 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200673 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400674 }
675 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200676
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200677 interfacesRemovedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500678 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500679 sdbusplus::bus::match::rules::interfacesRemoved(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200680 [this, probeInterfaces](sdbusplus::message_t& msg) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400681 if (irContainsProbeInterface(msg, probeInterfaces))
682 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200683 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400684 }
685 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200686}
Brad Bishopc76af0f2020-12-04 13:50:23 -0500687
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200688int main()
689{
Alexander Hansena555acf2025-06-27 11:59:10 +0200690 boost::asio::io_context io;
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200691 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
692 systemBus->request_name("xyz.openbmc_project.EntityManager");
Alexander Hansena555acf2025-06-27 11:59:10 +0200693 EntityManager em(systemBus, io);
James Feist4131aea2018-03-09 09:47:30 -0800694
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200695 nlohmann::json systemConfiguration = nlohmann::json::object();
696
697 std::set<std::string> probeInterfaces = configuration::getProbeInterfaces();
698
699 em.initFilters(probeInterfaces);
700
701 boost::asio::post(io, [&]() { em.propertiesChangedCallback(); });
James Feist8f2710a2018-05-09 17:18:55 -0700702
Alexander Hansenad28e402025-06-25 12:38:20 +0200703 em.handleCurrentConfigurationJson();
James Feist1df06a42019-04-11 14:23:04 -0700704
705 // some boards only show up after power is on, we want to not say they are
706 // removed until the same state happens
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200707 em_utils::setupPowerMatch(em.systemBus);
James Feist1df06a42019-04-11 14:23:04 -0700708
James Feist1b2e2242018-01-30 13:45:19 -0800709 io.run();
James Feist3cb5fec2018-01-23 14:41:51 -0800710
711 return 0;
James Feist75fdeeb2018-02-20 14:26:16 -0800712}