blob: ff9a5d5af1298b75619d13e6c89b15f55fce0874 [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);
Christopher Meisf7252572025-06-11 13:22:05 +020089
90 initFilters(configuration.probeInterfaces);
Christopher Meiscf6a75b2025-06-03 07:53:50 +020091}
92
93void EntityManager::postToDbus(const nlohmann::json& newConfiguration)
James Feist1b2e2242018-01-30 13:45:19 -080094{
Matt Spinler6eb60972023-08-14 16:36:20 -050095 std::map<std::string, std::string> newBoards; // path -> name
Benjamin Fairca2eb042022-09-13 06:40:42 +000096
James Feist97a63f12018-05-17 13:50:57 -070097 // iterate through boards
Patrick Williams2594d362022-09-28 06:46:24 -050098 for (const auto& [boardId, boardConfig] : newConfiguration.items())
James Feist1b2e2242018-01-30 13:45:19 -080099 {
Matt Spinler3d1909a2023-08-10 16:39:44 -0500100 std::string boardName = boardConfig["Name"];
101 std::string boardNameOrig = boardConfig["Name"];
Andrew Jeffery13132df2022-03-25 13:29:41 +1030102 std::string jsonPointerPath = "/" + boardId;
James Feist97a63f12018-05-17 13:50:57 -0700103 // loop through newConfiguration, but use values from system
104 // configuration to be able to modify via dbus later
Andrew Jeffery13132df2022-03-25 13:29:41 +1030105 auto boardValues = systemConfiguration[boardId];
James Feistd63d18a2018-07-19 15:23:45 -0700106 auto findBoardType = boardValues.find("Type");
James Feist1b2e2242018-01-30 13:45:19 -0800107 std::string boardType;
108 if (findBoardType != boardValues.end() &&
109 findBoardType->type() == nlohmann::json::value_t::string)
110 {
111 boardType = findBoardType->get<std::string>();
112 std::regex_replace(boardType.begin(), boardType.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800113 boardType.end(), illegalDbusMemberRegex, "_");
James Feist1b2e2242018-01-30 13:45:19 -0800114 }
115 else
116 {
Matt Spinler3d1909a2023-08-10 16:39:44 -0500117 std::cerr << "Unable to find type for " << boardName
James Feist1b2e2242018-01-30 13:45:19 -0800118 << " reverting to Chassis.\n";
119 boardType = "Chassis";
120 }
James Feist11be6672018-04-06 14:05:32 -0700121 std::string boardtypeLower = boost::algorithm::to_lower_copy(boardType);
James Feist1b2e2242018-01-30 13:45:19 -0800122
Matt Spinler3d1909a2023-08-10 16:39:44 -0500123 std::regex_replace(boardName.begin(), boardName.begin(),
124 boardName.end(), illegalDbusMemberRegex, "_");
125 std::string boardPath = "/xyz/openbmc_project/inventory/system/";
126 boardPath += boardtypeLower;
127 boardPath += "/";
128 boardPath += boardName;
James Feist1b2e2242018-01-30 13:45:19 -0800129
James Feistd58879a82019-09-11 11:26:07 -0700130 std::shared_ptr<sdbusplus::asio::dbus_interface> inventoryIface =
Alexander Hansen57604ed2025-06-27 13:22:28 +0200131 dbus_interface.createInterface(objServer, boardPath,
132 "xyz.openbmc_project.Inventory.Item",
133 boardName);
James Feist68500ff2018-08-08 15:40:42 -0700134
James Feistd58879a82019-09-11 11:26:07 -0700135 std::shared_ptr<sdbusplus::asio::dbus_interface> boardIface =
Alexander Hansen57604ed2025-06-27 13:22:28 +0200136 dbus_interface.createInterface(
Christopher Meis12bea9b2025-04-03 10:14:42 +0200137 objServer, boardPath,
138 "xyz.openbmc_project.Inventory.Item." + boardType,
139 boardNameOrig);
James Feist11be6672018-04-06 14:05:32 -0700140
Alexander Hansen57604ed2025-06-27 13:22:28 +0200141 dbus_interface.createAddObjectMethod(
Alexander Hansena555acf2025-06-27 11:59:10 +0200142 io, jsonPointerPath, boardPath, systemConfiguration, objServer,
Christopher Meis12bea9b2025-04-03 10:14:42 +0200143 boardNameOrig);
James Feist68500ff2018-08-08 15:40:42 -0700144
Christopher Meis12bea9b2025-04-03 10:14:42 +0200145 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200146 io, systemConfiguration, jsonPointerPath, boardIface, boardValues,
Christopher Meis12bea9b2025-04-03 10:14:42 +0200147 objServer);
James Feist97a63f12018-05-17 13:50:57 -0700148 jsonPointerPath += "/";
149 // iterate through board properties
Patrick Williams2594d362022-09-28 06:46:24 -0500150 for (const auto& [propName, propValue] : boardValues.items())
James Feist11be6672018-04-06 14:05:32 -0700151 {
Andrew Jefferya96950d2022-03-25 13:32:46 +1030152 if (propValue.type() == nlohmann::json::value_t::object)
James Feist11be6672018-04-06 14:05:32 -0700153 {
James Feistd58879a82019-09-11 11:26:07 -0700154 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
Alexander Hansen57604ed2025-06-27 13:22:28 +0200155 dbus_interface.createInterface(objServer, boardPath,
156 propName, boardNameOrig);
James Feistd58879a82019-09-11 11:26:07 -0700157
Christopher Meis12bea9b2025-04-03 10:14:42 +0200158 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200159 io, systemConfiguration, jsonPointerPath + propName, iface,
Christopher Meis12bea9b2025-04-03 10:14:42 +0200160 propValue, objServer);
James Feist11be6672018-04-06 14:05:32 -0700161 }
162 }
James Feist97a63f12018-05-17 13:50:57 -0700163
James Feist1e3e6982018-08-03 16:09:28 -0700164 auto exposes = boardValues.find("Exposes");
James Feist1b2e2242018-01-30 13:45:19 -0800165 if (exposes == boardValues.end())
166 {
167 continue;
168 }
James Feist97a63f12018-05-17 13:50:57 -0700169 // iterate through exposes
James Feist1e3e6982018-08-03 16:09:28 -0700170 jsonPointerPath += "Exposes/";
James Feist97a63f12018-05-17 13:50:57 -0700171
172 // store the board level pointer so we can modify it on the way down
173 std::string jsonPointerPathBoard = jsonPointerPath;
174 size_t exposesIndex = -1;
James Feista465ccc2019-02-08 12:51:01 -0800175 for (auto& item : *exposes)
James Feist1b2e2242018-01-30 13:45:19 -0800176 {
James Feist97a63f12018-05-17 13:50:57 -0700177 exposesIndex++;
178 jsonPointerPath = jsonPointerPathBoard;
179 jsonPointerPath += std::to_string(exposesIndex);
180
James Feistd63d18a2018-07-19 15:23:45 -0700181 auto findName = item.find("Name");
James Feist1b2e2242018-01-30 13:45:19 -0800182 if (findName == item.end())
183 {
184 std::cerr << "cannot find name in field " << item << "\n";
185 continue;
186 }
James Feist1e3e6982018-08-03 16:09:28 -0700187 auto findStatus = item.find("Status");
James Feist1b2e2242018-01-30 13:45:19 -0800188 // if status is not found it is assumed to be status = 'okay'
189 if (findStatus != item.end())
190 {
191 if (*findStatus == "disabled")
192 {
193 continue;
194 }
195 }
James Feistd63d18a2018-07-19 15:23:45 -0700196 auto findType = item.find("Type");
James Feist1b2e2242018-01-30 13:45:19 -0800197 std::string itemType;
198 if (findType != item.end())
199 {
200 itemType = findType->get<std::string>();
201 std::regex_replace(itemType.begin(), itemType.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800202 itemType.end(), illegalDbusPathRegex, "_");
James Feist1b2e2242018-01-30 13:45:19 -0800203 }
204 else
205 {
206 itemType = "unknown";
207 }
208 std::string itemName = findName->get<std::string>();
209 std::regex_replace(itemName.begin(), itemName.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800210 itemName.end(), illegalDbusMemberRegex, "_");
Matt Spinler3d1909a2023-08-10 16:39:44 -0500211 std::string ifacePath = boardPath;
Ed Tanous07d467b2021-02-23 14:48:37 -0800212 ifacePath += "/";
213 ifacePath += itemName;
James Feistc6248a52018-08-14 10:09:45 -0700214
Sui Chen74ebe592022-09-13 10:22:03 -0700215 if (itemType == "BMC")
216 {
217 std::shared_ptr<sdbusplus::asio::dbus_interface> bmcIface =
Alexander Hansen57604ed2025-06-27 13:22:28 +0200218 dbus_interface.createInterface(
Christopher Meis12bea9b2025-04-03 10:14:42 +0200219 objServer, ifacePath,
220 "xyz.openbmc_project.Inventory.Item.Bmc",
221 boardNameOrig);
222 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200223 io, systemConfiguration, jsonPointerPath, bmcIface, item,
Christopher Meis12bea9b2025-04-03 10:14:42 +0200224 objServer, getPermission(itemType));
Sui Chen74ebe592022-09-13 10:22:03 -0700225 }
Edward Leeeb587b42023-03-08 18:59:04 +0000226 else if (itemType == "System")
227 {
228 std::shared_ptr<sdbusplus::asio::dbus_interface> systemIface =
Alexander Hansen57604ed2025-06-27 13:22:28 +0200229 dbus_interface.createInterface(
Christopher Meis12bea9b2025-04-03 10:14:42 +0200230 objServer, ifacePath,
231 "xyz.openbmc_project.Inventory.Item.System",
232 boardNameOrig);
233 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200234 io, systemConfiguration, jsonPointerPath, systemIface, item,
Christopher Meis12bea9b2025-04-03 10:14:42 +0200235 objServer, getPermission(itemType));
Edward Leeeb587b42023-03-08 18:59:04 +0000236 }
Sui Chen74ebe592022-09-13 10:22:03 -0700237
Patrick Williams2594d362022-09-28 06:46:24 -0500238 for (const auto& [name, config] : item.items())
James Feist1b2e2242018-01-30 13:45:19 -0800239 {
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030240 jsonPointerPath = jsonPointerPathBoard;
241 jsonPointerPath.append(std::to_string(exposesIndex))
242 .append("/")
243 .append(name);
244 if (config.type() == nlohmann::json::value_t::object)
James Feist1b2e2242018-01-30 13:45:19 -0800245 {
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030246 std::string ifaceName =
247 "xyz.openbmc_project.Configuration.";
248 ifaceName.append(itemType).append(".").append(name);
James Feist97a63f12018-05-17 13:50:57 -0700249
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030250 std::shared_ptr<sdbusplus::asio::dbus_interface>
Alexander Hansen57604ed2025-06-27 13:22:28 +0200251 objectIface = dbus_interface.createInterface(
Christopher Meis12bea9b2025-04-03 10:14:42 +0200252 objServer, ifacePath, ifaceName, boardNameOrig);
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030253
Christopher Meis12bea9b2025-04-03 10:14:42 +0200254 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200255 io, systemConfiguration, jsonPointerPath, objectIface,
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030256 config, objServer, getPermission(name));
James Feist1b2e2242018-01-30 13:45:19 -0800257 }
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030258 else if (config.type() == nlohmann::json::value_t::array)
James Feist1b2e2242018-01-30 13:45:19 -0800259 {
260 size_t index = 0;
Ed Tanous3013fb42022-07-09 08:27:06 -0700261 if (config.empty())
James Feist1b2e2242018-01-30 13:45:19 -0800262 {
James Feist8f2710a2018-05-09 17:18:55 -0700263 continue;
264 }
265 bool isLegal = true;
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030266 auto type = config[0].type();
James Feist8f2710a2018-05-09 17:18:55 -0700267 if (type != nlohmann::json::value_t::object)
268 {
269 continue;
270 }
271
272 // verify legal json
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030273 for (const auto& arrayItem : config)
James Feist8f2710a2018-05-09 17:18:55 -0700274 {
275 if (arrayItem.type() != type)
James Feist1b2e2242018-01-30 13:45:19 -0800276 {
James Feist8f2710a2018-05-09 17:18:55 -0700277 isLegal = false;
James Feist1b2e2242018-01-30 13:45:19 -0800278 break;
279 }
James Feist8f2710a2018-05-09 17:18:55 -0700280 }
281 if (!isLegal)
282 {
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030283 std::cerr << "dbus format error" << config << "\n";
James Feist8f2710a2018-05-09 17:18:55 -0700284 break;
285 }
286
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030287 for (auto& arrayItem : config)
James Feist8f2710a2018-05-09 17:18:55 -0700288 {
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030289 std::string ifaceName =
290 "xyz.openbmc_project.Configuration.";
291 ifaceName.append(itemType).append(".").append(name);
292 ifaceName.append(std::to_string(index));
James Feist97a63f12018-05-17 13:50:57 -0700293
James Feistd58879a82019-09-11 11:26:07 -0700294 std::shared_ptr<sdbusplus::asio::dbus_interface>
Alexander Hansen57604ed2025-06-27 13:22:28 +0200295 objectIface = dbus_interface.createInterface(
Matt Spinler3d1909a2023-08-10 16:39:44 -0500296 objServer, ifacePath, ifaceName, boardNameOrig);
James Feistd58879a82019-09-11 11:26:07 -0700297
Christopher Meis12bea9b2025-04-03 10:14:42 +0200298 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200299 io, systemConfiguration,
James Feistc6248a52018-08-14 10:09:45 -0700300 jsonPointerPath + "/" + std::to_string(index),
301 objectIface, arrayItem, objServer,
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030302 getPermission(name));
James Feistbb43d022018-06-12 15:44:33 -0700303 index++;
James Feist1b2e2242018-01-30 13:45:19 -0800304 }
305 }
306 }
Benjamin Fairca2eb042022-09-13 06:40:42 +0000307
George Liu5c1a61a2024-10-24 18:02:29 +0800308 std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface =
Alexander Hansen57604ed2025-06-27 13:22:28 +0200309 dbus_interface.createInterface(
Christopher Meis12bea9b2025-04-03 10:14:42 +0200310 objServer, ifacePath,
311 "xyz.openbmc_project.Configuration." + itemType,
312 boardNameOrig);
George Liu5c1a61a2024-10-24 18:02:29 +0800313
Christopher Meis12bea9b2025-04-03 10:14:42 +0200314 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200315 io, systemConfiguration, jsonPointerPath, itemIface, item,
Christopher Meis12bea9b2025-04-03 10:14:42 +0200316 objServer, getPermission(itemType));
George Liu5c1a61a2024-10-24 18:02:29 +0800317
Matt Spinler6eb60972023-08-14 16:36:20 -0500318 topology.addBoard(boardPath, boardType, boardNameOrig, item);
James Feist1b2e2242018-01-30 13:45:19 -0800319 }
Matt Spinler6eb60972023-08-14 16:36:20 -0500320
321 newBoards.emplace(boardPath, boardNameOrig);
James Feist1b2e2242018-01-30 13:45:19 -0800322 }
Benjamin Fairca2eb042022-09-13 06:40:42 +0000323
Matt Spinler6eb60972023-08-14 16:36:20 -0500324 for (const auto& [assocPath, assocPropValue] :
325 topology.getAssocs(newBoards))
Benjamin Fairca2eb042022-09-13 06:40:42 +0000326 {
Matt Spinler6eb60972023-08-14 16:36:20 -0500327 auto findBoard = newBoards.find(assocPath);
328 if (findBoard == newBoards.end())
329 {
330 continue;
331 }
Benjamin Fairca2eb042022-09-13 06:40:42 +0000332
Alexander Hansen57604ed2025-06-27 13:22:28 +0200333 auto ifacePtr = dbus_interface.createInterface(
Matt Spinler6eb60972023-08-14 16:36:20 -0500334 objServer, assocPath, "xyz.openbmc_project.Association.Definitions",
335 findBoard->second);
336
337 ifacePtr->register_property("Associations", assocPropValue);
Christopher Meis12bea9b2025-04-03 10:14:42 +0200338 dbus_interface::tryIfaceInitialize(ifacePtr);
Benjamin Fairca2eb042022-09-13 06:40:42 +0000339 }
James Feist1b2e2242018-01-30 13:45:19 -0800340}
341
Andrew Jeffery55192932022-03-24 12:29:27 +1030342static bool deviceRequiresPowerOn(const nlohmann::json& entity)
343{
344 auto powerState = entity.find("PowerState");
Andrew Jefferyb6209442022-03-24 12:36:20 +1030345 if (powerState == entity.end())
Andrew Jeffery55192932022-03-24 12:29:27 +1030346 {
Andrew Jefferyb6209442022-03-24 12:36:20 +1030347 return false;
Andrew Jeffery55192932022-03-24 12:29:27 +1030348 }
349
Ed Tanous3013fb42022-07-09 08:27:06 -0700350 const auto* ptr = powerState->get_ptr<const std::string*>();
351 if (ptr == nullptr)
Andrew Jefferyb6209442022-03-24 12:36:20 +1030352 {
353 return false;
354 }
355
356 return *ptr == "On" || *ptr == "BiosPost";
Andrew Jeffery55192932022-03-24 12:29:27 +1030357}
358
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030359static void pruneDevice(const nlohmann::json& systemConfiguration,
360 const bool powerOff, const bool scannedPowerOff,
361 const std::string& name, const nlohmann::json& device)
362{
363 if (systemConfiguration.contains(name))
364 {
365 return;
366 }
367
Andrew Jeffery4db38bc2022-03-24 13:42:41 +1030368 if (deviceRequiresPowerOn(device) && (powerOff || scannedPowerOff))
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030369 {
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030370 return;
371 }
372
373 logDeviceRemoved(device);
374}
375
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200376void EntityManager::startRemovedTimer(boost::asio::steady_timer& timer,
377 nlohmann::json& systemConfiguration)
James Feist1df06a42019-04-11 14:23:04 -0700378{
James Feistfb00f392019-06-25 14:16:48 -0700379 if (systemConfiguration.empty() || lastJson.empty())
380 {
381 return; // not ready yet
382 }
James Feist1df06a42019-04-11 14:23:04 -0700383 if (scannedPowerOn)
384 {
385 return;
386 }
387
Christopher Meis59ef1e72025-04-16 08:53:25 +0200388 if (!em_utils::isPowerOn() && scannedPowerOff)
James Feist1df06a42019-04-11 14:23:04 -0700389 {
390 return;
391 }
392
James Feistb1728ca2020-04-30 15:40:55 -0700393 timer.expires_after(std::chrono::seconds(10));
Andrew Jeffery27a1cfb2022-03-24 12:31:53 +1030394 timer.async_wait(
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200395 [&systemConfiguration, this](const boost::system::error_code& ec) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400396 if (ec == boost::asio::error::operation_aborted)
397 {
398 return;
399 }
Andrew Jeffery27a1cfb2022-03-24 12:31:53 +1030400
Christopher Meis59ef1e72025-04-16 08:53:25 +0200401 bool powerOff = !em_utils::isPowerOn();
Patrick Williamsb7077432024-08-16 15:22:21 -0400402 for (const auto& [name, device] : lastJson.items())
403 {
404 pruneDevice(systemConfiguration, powerOff, scannedPowerOff,
405 name, device);
406 }
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030407
Patrick Williamsb7077432024-08-16 15:22:21 -0400408 scannedPowerOff = true;
409 if (!powerOff)
410 {
411 scannedPowerOn = true;
412 }
413 });
James Feist1df06a42019-04-11 14:23:04 -0700414}
415
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200416void EntityManager::pruneConfiguration(bool powerOff, const std::string& name,
417 const nlohmann::json& device)
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030418{
419 if (powerOff && deviceRequiresPowerOn(device))
420 {
421 // power not on yet, don't know if it's there or not
422 return;
423 }
424
Alexander Hansen57604ed2025-06-27 13:22:28 +0200425 auto& ifaces = dbus_interface.getDeviceInterfaces(device);
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030426 for (auto& iface : ifaces)
427 {
428 auto sharedPtr = iface.lock();
429 if (!!sharedPtr)
430 {
431 objServer.remove_interface(sharedPtr);
432 }
433 }
434
435 ifaces.clear();
436 systemConfiguration.erase(name);
Matt Spinler6eb60972023-08-14 16:36:20 -0500437 topology.remove(device["Name"].get<std::string>());
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030438 logDeviceRemoved(device);
439}
440
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200441void EntityManager::publishNewConfiguration(
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030442 const size_t& instance, const size_t count,
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200443 boost::asio::steady_timer& timer, // Gerrit discussion:
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030444 // https://gerrit.openbmc-project.xyz/c/openbmc/entity-manager/+/52316/6
445 //
446 // Discord discussion:
447 // https://discord.com/channels/775381525260664832/867820390406422538/958048437729910854
448 //
449 // NOLINTNEXTLINE(performance-unnecessary-value-param)
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200450 const nlohmann::json newConfiguration)
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030451{
Alexander Hansena555acf2025-06-27 11:59:10 +0200452 loadOverlays(newConfiguration, io);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030453
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200454 boost::asio::post(io, [this]() {
Christopher Meisf7252572025-06-11 13:22:05 +0200455 if (!writeJsonFiles(systemConfiguration))
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030456 {
457 std::cerr << "Error writing json files\n";
458 }
459 });
460
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200461 boost::asio::post(io, [this, &instance, count, &timer, newConfiguration]() {
462 postToDbus(newConfiguration);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030463 if (count == instance)
464 {
Alexander Hansen95ab18f2025-06-27 13:58:10 +0200465 startRemovedTimer(timer, systemConfiguration);
Andrew Jefferye35d0ac2022-03-24 15:53:13 +1030466 }
467 });
468}
469
James Feist8f2710a2018-05-09 17:18:55 -0700470// main properties changed entry
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200471void EntityManager::propertiesChangedCallback()
James Feist8f2710a2018-05-09 17:18:55 -0700472{
Alexander Hansenfc1b1e22025-06-27 14:34:11 +0200473 propertiesChangedInstance++;
474 size_t count = propertiesChangedInstance;
James Feist1df06a42019-04-11 14:23:04 -0700475
Alexander Hansenb1340da2025-06-27 14:29:13 +0200476 propertiesChangedTimer.expires_after(std::chrono::milliseconds(500));
James Feist8f2710a2018-05-09 17:18:55 -0700477
478 // setup an async wait as we normally get flooded with new requests
Alexander Hansenb1340da2025-06-27 14:29:13 +0200479 propertiesChangedTimer.async_wait(
480 [this, count](const boost::system::error_code& ec) {
481 if (ec == boost::asio::error::operation_aborted)
482 {
483 // we were cancelled
484 return;
485 }
486 if (ec)
487 {
488 std::cerr << "async wait error " << ec << "\n";
489 return;
490 }
James Feist8f2710a2018-05-09 17:18:55 -0700491
Alexander Hansenb1340da2025-06-27 14:29:13 +0200492 if (propertiesChangedInProgress)
493 {
494 propertiesChangedCallback();
495 return;
496 }
497 propertiesChangedInProgress = true;
James Feist2539ccd2020-05-01 16:15:08 -0700498
Alexander Hansenb1340da2025-06-27 14:29:13 +0200499 nlohmann::json oldConfiguration = systemConfiguration;
500 auto missingConfigurations = std::make_shared<nlohmann::json>();
501 *missingConfigurations = systemConfiguration;
James Feist899e17f2019-09-13 11:46:29 -0700502
Alexander Hansenb1340da2025-06-27 14:29:13 +0200503 auto perfScan = std::make_shared<scan::PerformScan>(
Christopher Meisf7252572025-06-11 13:22:05 +0200504 *this, *missingConfigurations, configuration.configurations, io,
Alexander Hansenb1340da2025-06-27 14:29:13 +0200505 [this, count, oldConfiguration, missingConfigurations]() {
506 // this is something that since ac has been applied to the
507 // bmc we saw, and we no longer see it
508 bool powerOff = !em_utils::isPowerOn();
509 for (const auto& [name, device] :
510 missingConfigurations->items())
511 {
512 pruneConfiguration(powerOff, name, device);
513 }
Alexander Hansenb1340da2025-06-27 14:29:13 +0200514 nlohmann::json newConfiguration = systemConfiguration;
515
Christopher Meisf7252572025-06-11 13:22:05 +0200516 deriveNewConfiguration(oldConfiguration, newConfiguration);
Alexander Hansenb1340da2025-06-27 14:29:13 +0200517
518 for (const auto& [_, device] : newConfiguration.items())
519 {
520 logDeviceAdded(device);
521 }
522
523 propertiesChangedInProgress = false;
524
525 boost::asio::post(io, [this, newConfiguration, count] {
526 publishNewConfiguration(
Alexander Hansenfc1b1e22025-06-27 14:34:11 +0200527 std::ref(propertiesChangedInstance), count,
Alexander Hansenb1340da2025-06-27 14:29:13 +0200528 std::ref(propertiesChangedTimer), newConfiguration);
529 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200530 });
Alexander Hansenb1340da2025-06-27 14:29:13 +0200531 perfScan->run();
532 });
James Feist75fdeeb2018-02-20 14:26:16 -0800533}
534
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600535// Check if InterfacesAdded payload contains an iface that needs probing.
Patrick Williamsb7077432024-08-16 15:22:21 -0400536static bool iaContainsProbeInterface(
Christopher Meisf7252572025-06-11 13:22:05 +0200537 sdbusplus::message_t& msg,
538 const std::unordered_set<std::string>& probeInterfaces)
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600539{
540 sdbusplus::message::object_path path;
541 DBusObject interfaces;
542 std::set<std::string> interfaceSet;
543 std::set<std::string> intersect;
544
545 msg.read(path, interfaces);
546
547 std::for_each(interfaces.begin(), interfaces.end(),
548 [&interfaceSet](const auto& iface) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400549 interfaceSet.insert(iface.first);
550 });
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600551
552 std::set_intersection(interfaceSet.begin(), interfaceSet.end(),
553 probeInterfaces.begin(), probeInterfaces.end(),
554 std::inserter(intersect, intersect.end()));
555 return !intersect.empty();
556}
557
558// Check if InterfacesRemoved payload contains an iface that needs probing.
Patrick Williamsb7077432024-08-16 15:22:21 -0400559static bool irContainsProbeInterface(
Christopher Meisf7252572025-06-11 13:22:05 +0200560 sdbusplus::message_t& msg,
561 const std::unordered_set<std::string>& probeInterfaces)
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600562{
563 sdbusplus::message::object_path path;
564 std::set<std::string> interfaces;
565 std::set<std::string> intersect;
566
567 msg.read(path, interfaces);
568
569 std::set_intersection(interfaces.begin(), interfaces.end(),
570 probeInterfaces.begin(), probeInterfaces.end(),
571 std::inserter(intersect, intersect.end()));
572 return !intersect.empty();
573}
574
Alexander Hansenad28e402025-06-25 12:38:20 +0200575void EntityManager::handleCurrentConfigurationJson()
576{
577 if (em_utils::fwVersionIsSame())
578 {
Christopher Meisf7252572025-06-11 13:22:05 +0200579 if (std::filesystem::is_regular_file(currentConfiguration))
Alexander Hansenad28e402025-06-25 12:38:20 +0200580 {
581 // this file could just be deleted, but it's nice for debug
582 std::filesystem::create_directory(tempConfigDir);
583 std::filesystem::remove(lastConfiguration);
Christopher Meisf7252572025-06-11 13:22:05 +0200584 std::filesystem::copy(currentConfiguration, lastConfiguration);
585 std::filesystem::remove(currentConfiguration);
Alexander Hansenad28e402025-06-25 12:38:20 +0200586
587 std::ifstream jsonStream(lastConfiguration);
588 if (jsonStream.good())
589 {
590 auto data = nlohmann::json::parse(jsonStream, nullptr, false);
591 if (data.is_discarded())
592 {
593 std::cerr
594 << "syntax error in " << lastConfiguration << "\n";
595 }
596 else
597 {
598 lastJson = std::move(data);
599 }
600 }
601 else
602 {
603 std::cerr << "unable to open " << lastConfiguration << "\n";
604 }
605 }
606 }
607 else
608 {
609 // not an error, just logging at this level to make it in the journal
610 std::cerr << "Clearing previous configuration\n";
Christopher Meisf7252572025-06-11 13:22:05 +0200611 std::filesystem::remove(currentConfiguration);
Alexander Hansenad28e402025-06-25 12:38:20 +0200612 }
613}
614
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200615void EntityManager::registerCallback(const std::string& path)
James Feist75fdeeb2018-02-20 14:26:16 -0800616{
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200617 if (dbusMatches.contains(path))
618 {
619 return;
620 }
Nan Zhoua3315672022-09-20 19:48:14 +0000621
Alexander Hansen60803182025-06-27 14:41:28 +0200622 lg2::debug("creating PropertiesChanged match on {PATH}", "PATH", path);
623
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200624 std::function<void(sdbusplus::message_t & message)> eventHandler =
625 [&](sdbusplus::message_t&) { propertiesChangedCallback(); };
James Feistfd1264a2018-05-03 12:10:00 -0700626
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200627 sdbusplus::bus::match_t match(
628 static_cast<sdbusplus::bus_t&>(*systemBus),
629 "type='signal',member='PropertiesChanged',path='" + path + "'",
630 eventHandler);
631 dbusMatches.emplace(path, std::move(match));
632}
James Feistfd1264a2018-05-03 12:10:00 -0700633
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200634// We need a poke from DBus for static providers that create all their
635// objects prior to claiming a well-known name, and thus don't emit any
636// org.freedesktop.DBus.Properties signals. Similarly if a process exits
637// for any reason, expected or otherwise, we'll need a poke to remove
638// entities from DBus.
Christopher Meisf7252572025-06-11 13:22:05 +0200639void EntityManager::initFilters(
640 const std::unordered_set<std::string>& probeInterfaces)
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200641{
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200642 nameOwnerChangedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500643 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishopc76af0f2020-12-04 13:50:23 -0500644 sdbusplus::bus::match::rules::nameOwnerChanged(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200645 [this](sdbusplus::message_t& m) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400646 auto [name, oldOwner,
647 newOwner] = m.unpack<std::string, std::string, std::string>();
Patrick Williams7b8786f2022-10-10 10:23:37 -0500648
Patrick Williamsb7077432024-08-16 15:22:21 -0400649 if (name.starts_with(':'))
650 {
651 // We should do nothing with unique-name connections.
652 return;
653 }
Patrick Williams7b8786f2022-10-10 10:23:37 -0500654
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200655 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400656 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200657
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500658 // We also need a poke from DBus when new interfaces are created or
659 // destroyed.
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200660 interfacesAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500661 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500662 sdbusplus::bus::match::rules::interfacesAdded(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200663 [this, probeInterfaces](sdbusplus::message_t& msg) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400664 if (iaContainsProbeInterface(msg, probeInterfaces))
665 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200666 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400667 }
668 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200669
Alexander Hansen0f7bd782025-06-27 13:39:53 +0200670 interfacesRemovedMatch = std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams2af39222022-07-22 19:26:56 -0500671 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500672 sdbusplus::bus::match::rules::interfacesRemoved(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200673 [this, probeInterfaces](sdbusplus::message_t& msg) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400674 if (irContainsProbeInterface(msg, probeInterfaces))
675 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200676 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400677 }
678 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200679}
Brad Bishopc76af0f2020-12-04 13:50:23 -0500680
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200681int main()
682{
Alexander Hansena555acf2025-06-27 11:59:10 +0200683 boost::asio::io_context io;
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200684 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
685 systemBus->request_name("xyz.openbmc_project.EntityManager");
Alexander Hansena555acf2025-06-27 11:59:10 +0200686 EntityManager em(systemBus, io);
James Feist4131aea2018-03-09 09:47:30 -0800687
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200688 nlohmann::json systemConfiguration = nlohmann::json::object();
689
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200690 boost::asio::post(io, [&]() { em.propertiesChangedCallback(); });
James Feist8f2710a2018-05-09 17:18:55 -0700691
Alexander Hansenad28e402025-06-25 12:38:20 +0200692 em.handleCurrentConfigurationJson();
James Feist1df06a42019-04-11 14:23:04 -0700693
694 // some boards only show up after power is on, we want to not say they are
695 // removed until the same state happens
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200696 em_utils::setupPowerMatch(em.systemBus);
James Feist1df06a42019-04-11 14:23:04 -0700697
James Feist1b2e2242018-01-30 13:45:19 -0800698 io.run();
James Feist3cb5fec2018-01-23 14:41:51 -0800699
700 return 0;
James Feist75fdeeb2018-02-20 14:26:16 -0800701}