blob: e4cd71ae63aadfdaa9bea2a7441ec0a880d99497 [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"
Benjamin Fairca2eb042022-09-13 06:40:42 +000026#include "topology.hpp"
Christopher Meis59ef1e72025-04-16 08:53:25 +020027#include "utils.hpp"
James Feist481c5d52019-08-13 14:40:40 -070028
James Feist11be6672018-04-06 14:05:32 -070029#include <boost/algorithm/string/case_conv.hpp>
James Feistf5125b02019-06-06 11:27:43 -070030#include <boost/algorithm/string/classification.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080031#include <boost/algorithm/string/predicate.hpp>
32#include <boost/algorithm/string/replace.hpp>
James Feistf5125b02019-06-06 11:27:43 -070033#include <boost/algorithm/string/split.hpp>
James Feist02d2b932020-02-06 16:28:48 -080034#include <boost/asio/io_context.hpp>
Ed Tanous49a888c2023-03-06 13:44:51 -080035#include <boost/asio/post.hpp>
James Feistb1728ca2020-04-30 15:40:55 -070036#include <boost/asio/steady_timer.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080037#include <boost/container/flat_map.hpp>
38#include <boost/container/flat_set.hpp>
James Feistf5125b02019-06-06 11:27:43 -070039#include <boost/range/iterator_range.hpp>
James Feist8c505da2020-05-28 10:06:33 -070040#include <nlohmann/json.hpp>
41#include <sdbusplus/asio/connection.hpp>
42#include <sdbusplus/asio/object_server.hpp>
43
James Feist637b3ef2019-04-15 16:35:30 -070044#include <filesystem>
James Feista465ccc2019-02-08 12:51:01 -080045#include <fstream>
Andrew Jefferye35d0ac2022-03-24 15:53:13 +103046#include <functional>
James Feista465ccc2019-02-08 12:51:01 -080047#include <iostream>
Andrew Jeffery666583b2021-12-01 15:50:12 +103048#include <map>
James Feista465ccc2019-02-08 12:51:01 -080049#include <regex>
James Feist1df06a42019-04-11 14:23:04 -070050constexpr const char* tempConfigDir = "/tmp/configuration/";
51constexpr const char* lastConfiguration = "/tmp/configuration/last.json";
James Feistf1b14142019-04-10 15:22:09 -070052
Adrian Ambrożewiczc789fca2020-05-14 15:50:05 +020053static constexpr std::array<const char*, 6> settableInterfaces = {
54 "FanProfile", "Pid", "Pid.Zone", "Stepwise", "Thresholds", "Polling"};
James Feist3cb5fec2018-01-23 14:41:51 -080055
Ed Tanous07d467b2021-02-23 14:48:37 -080056const std::regex illegalDbusPathRegex("[^A-Za-z0-9_.]");
57const std::regex illegalDbusMemberRegex("[^A-Za-z0-9_]");
James Feist1b2e2242018-01-30 13:45:19 -080058
James Feista465ccc2019-02-08 12:51:01 -080059sdbusplus::asio::PropertyPermission getPermission(const std::string& interface)
James Feistc6248a52018-08-14 10:09:45 -070060{
61 return std::find(settableInterfaces.begin(), settableInterfaces.end(),
62 interface) != settableInterfaces.end()
63 ? sdbusplus::asio::PropertyPermission::readWrite
64 : sdbusplus::asio::PropertyPermission::readOnly;
65}
66
Christopher Meiscf6a75b2025-06-03 07:53:50 +020067EntityManager::EntityManager(
Alexander Hansena555acf2025-06-27 11:59:10 +020068 std::shared_ptr<sdbusplus::asio::connection>& systemBus,
69 boost::asio::io_context& io) :
Christopher Meiscf6a75b2025-06-03 07:53:50 +020070 systemBus(systemBus),
71 objServer(sdbusplus::asio::object_server(systemBus, /*skipManager=*/true)),
72 lastJson(nlohmann::json::object()),
Alexander Hansena555acf2025-06-27 11:59:10 +020073 systemConfiguration(nlohmann::json::object()), io(io)
Christopher Meiscf6a75b2025-06-03 07:53:50 +020074{
75 // All other objects that EntityManager currently support are under the
76 // inventory subtree.
77 // See the discussion at
78 // https://discord.com/channels/775381525260664832/1018929092009144380
79 objServer.add_manager("/xyz/openbmc_project/inventory");
James Feist75fdeeb2018-02-20 14:26:16 -080080
Christopher Meiscf6a75b2025-06-03 07:53:50 +020081 entityIface = objServer.add_interface("/xyz/openbmc_project/EntityManager",
82 "xyz.openbmc_project.EntityManager");
83 entityIface->register_method("ReScan", [this]() {
84 propertiesChangedCallback();
85 });
86 dbus_interface::tryIfaceInitialize(entityIface);
87}
88
89void EntityManager::postToDbus(const nlohmann::json& newConfiguration)
James Feist1b2e2242018-01-30 13:45:19 -080090{
Matt Spinler6eb60972023-08-14 16:36:20 -050091 std::map<std::string, std::string> newBoards; // path -> name
Benjamin Fairca2eb042022-09-13 06:40:42 +000092
James Feist97a63f12018-05-17 13:50:57 -070093 // iterate through boards
Patrick Williams2594d362022-09-28 06:46:24 -050094 for (const auto& [boardId, boardConfig] : newConfiguration.items())
James Feist1b2e2242018-01-30 13:45:19 -080095 {
Matt Spinler3d1909a2023-08-10 16:39:44 -050096 std::string boardName = boardConfig["Name"];
97 std::string boardNameOrig = boardConfig["Name"];
Andrew Jeffery13132df2022-03-25 13:29:41 +103098 std::string jsonPointerPath = "/" + boardId;
James Feist97a63f12018-05-17 13:50:57 -070099 // loop through newConfiguration, but use values from system
100 // configuration to be able to modify via dbus later
Andrew Jeffery13132df2022-03-25 13:29:41 +1030101 auto boardValues = systemConfiguration[boardId];
James Feistd63d18a2018-07-19 15:23:45 -0700102 auto findBoardType = boardValues.find("Type");
James Feist1b2e2242018-01-30 13:45:19 -0800103 std::string boardType;
104 if (findBoardType != boardValues.end() &&
105 findBoardType->type() == nlohmann::json::value_t::string)
106 {
107 boardType = findBoardType->get<std::string>();
108 std::regex_replace(boardType.begin(), boardType.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800109 boardType.end(), illegalDbusMemberRegex, "_");
James Feist1b2e2242018-01-30 13:45:19 -0800110 }
111 else
112 {
Matt Spinler3d1909a2023-08-10 16:39:44 -0500113 std::cerr << "Unable to find type for " << boardName
James Feist1b2e2242018-01-30 13:45:19 -0800114 << " reverting to Chassis.\n";
115 boardType = "Chassis";
116 }
James Feist11be6672018-04-06 14:05:32 -0700117 std::string boardtypeLower = boost::algorithm::to_lower_copy(boardType);
James Feist1b2e2242018-01-30 13:45:19 -0800118
Matt Spinler3d1909a2023-08-10 16:39:44 -0500119 std::regex_replace(boardName.begin(), boardName.begin(),
120 boardName.end(), illegalDbusMemberRegex, "_");
121 std::string boardPath = "/xyz/openbmc_project/inventory/system/";
122 boardPath += boardtypeLower;
123 boardPath += "/";
124 boardPath += boardName;
James Feist1b2e2242018-01-30 13:45:19 -0800125
James Feistd58879a82019-09-11 11:26:07 -0700126 std::shared_ptr<sdbusplus::asio::dbus_interface> inventoryIface =
Alexander Hansen57604ed2025-06-27 13:22:28 +0200127 dbus_interface.createInterface(objServer, boardPath,
128 "xyz.openbmc_project.Inventory.Item",
129 boardName);
James Feist68500ff2018-08-08 15:40:42 -0700130
James Feistd58879a82019-09-11 11:26:07 -0700131 std::shared_ptr<sdbusplus::asio::dbus_interface> boardIface =
Alexander Hansen57604ed2025-06-27 13:22:28 +0200132 dbus_interface.createInterface(
Christopher Meis12bea9b2025-04-03 10:14:42 +0200133 objServer, boardPath,
134 "xyz.openbmc_project.Inventory.Item." + boardType,
135 boardNameOrig);
James Feist11be6672018-04-06 14:05:32 -0700136
Alexander Hansen57604ed2025-06-27 13:22:28 +0200137 dbus_interface.createAddObjectMethod(
Alexander Hansena555acf2025-06-27 11:59:10 +0200138 io, jsonPointerPath, boardPath, systemConfiguration, objServer,
Christopher Meis12bea9b2025-04-03 10:14:42 +0200139 boardNameOrig);
James Feist68500ff2018-08-08 15:40:42 -0700140
Christopher Meis12bea9b2025-04-03 10:14:42 +0200141 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200142 io, systemConfiguration, jsonPointerPath, boardIface, boardValues,
Christopher Meis12bea9b2025-04-03 10:14:42 +0200143 objServer);
James Feist97a63f12018-05-17 13:50:57 -0700144 jsonPointerPath += "/";
145 // iterate through board properties
Patrick Williams2594d362022-09-28 06:46:24 -0500146 for (const auto& [propName, propValue] : boardValues.items())
James Feist11be6672018-04-06 14:05:32 -0700147 {
Andrew Jefferya96950d2022-03-25 13:32:46 +1030148 if (propValue.type() == nlohmann::json::value_t::object)
James Feist11be6672018-04-06 14:05:32 -0700149 {
James Feistd58879a82019-09-11 11:26:07 -0700150 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
Alexander Hansen57604ed2025-06-27 13:22:28 +0200151 dbus_interface.createInterface(objServer, boardPath,
152 propName, boardNameOrig);
James Feistd58879a82019-09-11 11:26:07 -0700153
Christopher Meis12bea9b2025-04-03 10:14:42 +0200154 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200155 io, systemConfiguration, jsonPointerPath + propName, iface,
Christopher Meis12bea9b2025-04-03 10:14:42 +0200156 propValue, objServer);
James Feist11be6672018-04-06 14:05:32 -0700157 }
158 }
James Feist97a63f12018-05-17 13:50:57 -0700159
James Feist1e3e6982018-08-03 16:09:28 -0700160 auto exposes = boardValues.find("Exposes");
James Feist1b2e2242018-01-30 13:45:19 -0800161 if (exposes == boardValues.end())
162 {
163 continue;
164 }
James Feist97a63f12018-05-17 13:50:57 -0700165 // iterate through exposes
James Feist1e3e6982018-08-03 16:09:28 -0700166 jsonPointerPath += "Exposes/";
James Feist97a63f12018-05-17 13:50:57 -0700167
168 // store the board level pointer so we can modify it on the way down
169 std::string jsonPointerPathBoard = jsonPointerPath;
170 size_t exposesIndex = -1;
James Feista465ccc2019-02-08 12:51:01 -0800171 for (auto& item : *exposes)
James Feist1b2e2242018-01-30 13:45:19 -0800172 {
James Feist97a63f12018-05-17 13:50:57 -0700173 exposesIndex++;
174 jsonPointerPath = jsonPointerPathBoard;
175 jsonPointerPath += std::to_string(exposesIndex);
176
James Feistd63d18a2018-07-19 15:23:45 -0700177 auto findName = item.find("Name");
James Feist1b2e2242018-01-30 13:45:19 -0800178 if (findName == item.end())
179 {
180 std::cerr << "cannot find name in field " << item << "\n";
181 continue;
182 }
James Feist1e3e6982018-08-03 16:09:28 -0700183 auto findStatus = item.find("Status");
James Feist1b2e2242018-01-30 13:45:19 -0800184 // if status is not found it is assumed to be status = 'okay'
185 if (findStatus != item.end())
186 {
187 if (*findStatus == "disabled")
188 {
189 continue;
190 }
191 }
James Feistd63d18a2018-07-19 15:23:45 -0700192 auto findType = item.find("Type");
James Feist1b2e2242018-01-30 13:45:19 -0800193 std::string itemType;
194 if (findType != item.end())
195 {
196 itemType = findType->get<std::string>();
197 std::regex_replace(itemType.begin(), itemType.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800198 itemType.end(), illegalDbusPathRegex, "_");
James Feist1b2e2242018-01-30 13:45:19 -0800199 }
200 else
201 {
202 itemType = "unknown";
203 }
204 std::string itemName = findName->get<std::string>();
205 std::regex_replace(itemName.begin(), itemName.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800206 itemName.end(), illegalDbusMemberRegex, "_");
Matt Spinler3d1909a2023-08-10 16:39:44 -0500207 std::string ifacePath = boardPath;
Ed Tanous07d467b2021-02-23 14:48:37 -0800208 ifacePath += "/";
209 ifacePath += itemName;
James Feistc6248a52018-08-14 10:09:45 -0700210
Sui Chen74ebe592022-09-13 10:22:03 -0700211 if (itemType == "BMC")
212 {
213 std::shared_ptr<sdbusplus::asio::dbus_interface> bmcIface =
Alexander Hansen57604ed2025-06-27 13:22:28 +0200214 dbus_interface.createInterface(
Christopher Meis12bea9b2025-04-03 10:14:42 +0200215 objServer, ifacePath,
216 "xyz.openbmc_project.Inventory.Item.Bmc",
217 boardNameOrig);
218 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200219 io, systemConfiguration, jsonPointerPath, bmcIface, item,
Christopher Meis12bea9b2025-04-03 10:14:42 +0200220 objServer, getPermission(itemType));
Sui Chen74ebe592022-09-13 10:22:03 -0700221 }
Edward Leeeb587b42023-03-08 18:59:04 +0000222 else if (itemType == "System")
223 {
224 std::shared_ptr<sdbusplus::asio::dbus_interface> systemIface =
Alexander Hansen57604ed2025-06-27 13:22:28 +0200225 dbus_interface.createInterface(
Christopher Meis12bea9b2025-04-03 10:14:42 +0200226 objServer, ifacePath,
227 "xyz.openbmc_project.Inventory.Item.System",
228 boardNameOrig);
229 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200230 io, systemConfiguration, jsonPointerPath, systemIface, item,
Christopher Meis12bea9b2025-04-03 10:14:42 +0200231 objServer, getPermission(itemType));
Edward Leeeb587b42023-03-08 18:59:04 +0000232 }
Sui Chen74ebe592022-09-13 10:22:03 -0700233
Patrick Williams2594d362022-09-28 06:46:24 -0500234 for (const auto& [name, config] : item.items())
James Feist1b2e2242018-01-30 13:45:19 -0800235 {
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030236 jsonPointerPath = jsonPointerPathBoard;
237 jsonPointerPath.append(std::to_string(exposesIndex))
238 .append("/")
239 .append(name);
240 if (config.type() == nlohmann::json::value_t::object)
James Feist1b2e2242018-01-30 13:45:19 -0800241 {
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030242 std::string ifaceName =
243 "xyz.openbmc_project.Configuration.";
244 ifaceName.append(itemType).append(".").append(name);
James Feist97a63f12018-05-17 13:50:57 -0700245
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030246 std::shared_ptr<sdbusplus::asio::dbus_interface>
Alexander Hansen57604ed2025-06-27 13:22:28 +0200247 objectIface = dbus_interface.createInterface(
Christopher Meis12bea9b2025-04-03 10:14:42 +0200248 objServer, ifacePath, ifaceName, boardNameOrig);
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030249
Christopher Meis12bea9b2025-04-03 10:14:42 +0200250 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200251 io, systemConfiguration, jsonPointerPath, objectIface,
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030252 config, objServer, getPermission(name));
James Feist1b2e2242018-01-30 13:45:19 -0800253 }
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030254 else if (config.type() == nlohmann::json::value_t::array)
James Feist1b2e2242018-01-30 13:45:19 -0800255 {
256 size_t index = 0;
Ed Tanous3013fb42022-07-09 08:27:06 -0700257 if (config.empty())
James Feist1b2e2242018-01-30 13:45:19 -0800258 {
James Feist8f2710a2018-05-09 17:18:55 -0700259 continue;
260 }
261 bool isLegal = true;
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030262 auto type = config[0].type();
James Feist8f2710a2018-05-09 17:18:55 -0700263 if (type != nlohmann::json::value_t::object)
264 {
265 continue;
266 }
267
268 // verify legal json
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030269 for (const auto& arrayItem : config)
James Feist8f2710a2018-05-09 17:18:55 -0700270 {
271 if (arrayItem.type() != type)
James Feist1b2e2242018-01-30 13:45:19 -0800272 {
James Feist8f2710a2018-05-09 17:18:55 -0700273 isLegal = false;
James Feist1b2e2242018-01-30 13:45:19 -0800274 break;
275 }
James Feist8f2710a2018-05-09 17:18:55 -0700276 }
277 if (!isLegal)
278 {
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030279 std::cerr << "dbus format error" << config << "\n";
James Feist8f2710a2018-05-09 17:18:55 -0700280 break;
281 }
282
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030283 for (auto& arrayItem : config)
James Feist8f2710a2018-05-09 17:18:55 -0700284 {
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030285 std::string ifaceName =
286 "xyz.openbmc_project.Configuration.";
287 ifaceName.append(itemType).append(".").append(name);
288 ifaceName.append(std::to_string(index));
James Feist97a63f12018-05-17 13:50:57 -0700289
James Feistd58879a82019-09-11 11:26:07 -0700290 std::shared_ptr<sdbusplus::asio::dbus_interface>
Alexander Hansen57604ed2025-06-27 13:22:28 +0200291 objectIface = dbus_interface.createInterface(
Matt Spinler3d1909a2023-08-10 16:39:44 -0500292 objServer, ifacePath, ifaceName, boardNameOrig);
James Feistd58879a82019-09-11 11:26:07 -0700293
Christopher Meis12bea9b2025-04-03 10:14:42 +0200294 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200295 io, systemConfiguration,
James Feistc6248a52018-08-14 10:09:45 -0700296 jsonPointerPath + "/" + std::to_string(index),
297 objectIface, arrayItem, objServer,
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030298 getPermission(name));
James Feistbb43d022018-06-12 15:44:33 -0700299 index++;
James Feist1b2e2242018-01-30 13:45:19 -0800300 }
301 }
302 }
Benjamin Fairca2eb042022-09-13 06:40:42 +0000303
George Liu5c1a61a2024-10-24 18:02:29 +0800304 std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface =
Alexander Hansen57604ed2025-06-27 13:22:28 +0200305 dbus_interface.createInterface(
Christopher Meis12bea9b2025-04-03 10:14:42 +0200306 objServer, ifacePath,
307 "xyz.openbmc_project.Configuration." + itemType,
308 boardNameOrig);
George Liu5c1a61a2024-10-24 18:02:29 +0800309
Christopher Meis12bea9b2025-04-03 10:14:42 +0200310 dbus_interface::populateInterfaceFromJson(
Alexander Hansena555acf2025-06-27 11:59:10 +0200311 io, systemConfiguration, jsonPointerPath, itemIface, item,
Christopher Meis12bea9b2025-04-03 10:14:42 +0200312 objServer, getPermission(itemType));
George Liu5c1a61a2024-10-24 18:02:29 +0800313
Matt Spinler6eb60972023-08-14 16:36:20 -0500314 topology.addBoard(boardPath, boardType, boardNameOrig, item);
James Feist1b2e2242018-01-30 13:45:19 -0800315 }
Matt Spinler6eb60972023-08-14 16:36:20 -0500316
317 newBoards.emplace(boardPath, boardNameOrig);
James Feist1b2e2242018-01-30 13:45:19 -0800318 }
Benjamin Fairca2eb042022-09-13 06:40:42 +0000319
Matt Spinler6eb60972023-08-14 16:36:20 -0500320 for (const auto& [assocPath, assocPropValue] :
321 topology.getAssocs(newBoards))
Benjamin Fairca2eb042022-09-13 06:40:42 +0000322 {
Matt Spinler6eb60972023-08-14 16:36:20 -0500323 auto findBoard = newBoards.find(assocPath);
324 if (findBoard == newBoards.end())
325 {
326 continue;
327 }
Benjamin Fairca2eb042022-09-13 06:40:42 +0000328
Alexander Hansen57604ed2025-06-27 13:22:28 +0200329 auto ifacePtr = dbus_interface.createInterface(
Matt Spinler6eb60972023-08-14 16:36:20 -0500330 objServer, assocPath, "xyz.openbmc_project.Association.Definitions",
331 findBoard->second);
332
333 ifacePtr->register_property("Associations", assocPropValue);
Christopher Meis12bea9b2025-04-03 10:14:42 +0200334 dbus_interface::tryIfaceInitialize(ifacePtr);
Benjamin Fairca2eb042022-09-13 06:40:42 +0000335 }
James Feist1b2e2242018-01-30 13:45:19 -0800336}
337
Andrew Jeffery55192932022-03-24 12:29:27 +1030338static bool deviceRequiresPowerOn(const nlohmann::json& entity)
339{
340 auto powerState = entity.find("PowerState");
Andrew Jefferyb6209442022-03-24 12:36:20 +1030341 if (powerState == entity.end())
Andrew Jeffery55192932022-03-24 12:29:27 +1030342 {
Andrew Jefferyb6209442022-03-24 12:36:20 +1030343 return false;
Andrew Jeffery55192932022-03-24 12:29:27 +1030344 }
345
Ed Tanous3013fb42022-07-09 08:27:06 -0700346 const auto* ptr = powerState->get_ptr<const std::string*>();
347 if (ptr == nullptr)
Andrew Jefferyb6209442022-03-24 12:36:20 +1030348 {
349 return false;
350 }
351
352 return *ptr == "On" || *ptr == "BiosPost";
Andrew Jeffery55192932022-03-24 12:29:27 +1030353}
354
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030355static void pruneDevice(const nlohmann::json& systemConfiguration,
356 const bool powerOff, const bool scannedPowerOff,
357 const std::string& name, const nlohmann::json& device)
358{
359 if (systemConfiguration.contains(name))
360 {
361 return;
362 }
363
Andrew Jeffery4db38bc2022-03-24 13:42:41 +1030364 if (deviceRequiresPowerOn(device) && (powerOff || scannedPowerOff))
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030365 {
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030366 return;
367 }
368
369 logDeviceRemoved(device);
370}
371
James Feistb1728ca2020-04-30 15:40:55 -0700372void startRemovedTimer(boost::asio::steady_timer& timer,
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200373 nlohmann::json& systemConfiguration,
374 nlohmann::json& lastJson)
James Feist1df06a42019-04-11 14:23:04 -0700375{
376 static bool scannedPowerOff = false;
377 static bool scannedPowerOn = false;
378
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(
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200395 [&systemConfiguration, &lastJson](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 Meisbdaa6b22025-04-02 10:49:02 +0200455 if (!configuration::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 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200465 startRemovedTimer(timer, systemConfiguration, lastJson);
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{
James Feist2539ccd2020-05-01 16:15:08 -0700473 static bool inProgress = false;
James Feistb1728ca2020-04-30 15:40:55 -0700474 static boost::asio::steady_timer timer(io);
James Feist899e17f2019-09-13 11:46:29 -0700475 static size_t instance = 0;
476 instance++;
477 size_t count = instance;
James Feist1df06a42019-04-11 14:23:04 -0700478
Anupama B Rbf263982024-01-25 04:42:39 -0600479 timer.expires_after(std::chrono::milliseconds(500));
James Feist8f2710a2018-05-09 17:18:55 -0700480
481 // setup an async wait as we normally get flooded with new requests
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200482 timer.async_wait([this, count](const boost::system::error_code& ec) {
James Feist8f2710a2018-05-09 17:18:55 -0700483 if (ec == boost::asio::error::operation_aborted)
484 {
485 // we were cancelled
486 return;
487 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800488 if (ec)
James Feist8f2710a2018-05-09 17:18:55 -0700489 {
490 std::cerr << "async wait error " << ec << "\n";
491 return;
492 }
493
James Feist2539ccd2020-05-01 16:15:08 -0700494 if (inProgress)
495 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200496 propertiesChangedCallback();
James Feist2539ccd2020-05-01 16:15:08 -0700497 return;
498 }
499 inProgress = true;
500
James Feist8f2710a2018-05-09 17:18:55 -0700501 nlohmann::json oldConfiguration = systemConfiguration;
James Feist899e17f2019-09-13 11:46:29 -0700502 auto missingConfigurations = std::make_shared<nlohmann::json>();
503 *missingConfigurations = systemConfiguration;
504
James Feist8f2710a2018-05-09 17:18:55 -0700505 std::list<nlohmann::json> configurations;
Christopher Meisbdaa6b22025-04-02 10:49:02 +0200506 if (!configuration::loadConfigurations(configurations))
James Feist8f2710a2018-05-09 17:18:55 -0700507 {
Andrew Jefferyf3311792022-03-29 22:09:00 +1030508 std::cerr << "Could not load configurations\n";
James Feist2539ccd2020-05-01 16:15:08 -0700509 inProgress = false;
James Feist8f2710a2018-05-09 17:18:55 -0700510 return;
511 }
512
Christopher Meis26fbbd52025-03-26 14:55:06 +0100513 auto perfScan = std::make_shared<scan::PerformScan>(
Alexander Hansena555acf2025-06-27 11:59:10 +0200514 *this, *missingConfigurations, configurations, io,
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200515 [this, count, oldConfiguration, missingConfigurations]() {
Patrick Williamsb7077432024-08-16 15:22:21 -0400516 // this is something that since ac has been applied to the bmc
517 // we saw, and we no longer see it
Christopher Meis59ef1e72025-04-16 08:53:25 +0200518 bool powerOff = !em_utils::isPowerOn();
Patrick Williamsb7077432024-08-16 15:22:21 -0400519 for (const auto& [name, device] :
520 missingConfigurations->items())
521 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200522 pruneConfiguration(powerOff, name, device);
Patrick Williamsb7077432024-08-16 15:22:21 -0400523 }
James Feist899e17f2019-09-13 11:46:29 -0700524
Patrick Williamsb7077432024-08-16 15:22:21 -0400525 nlohmann::json newConfiguration = systemConfiguration;
Andrew Jeffery7c4cbbe2022-03-24 15:27:10 +1030526
Christopher Meisbdaa6b22025-04-02 10:49:02 +0200527 configuration::deriveNewConfiguration(oldConfiguration,
528 newConfiguration);
Andrew Jeffery7c4cbbe2022-03-24 15:27:10 +1030529
Patrick Williamsb7077432024-08-16 15:22:21 -0400530 for (const auto& [_, device] : newConfiguration.items())
531 {
532 logDeviceAdded(device);
533 }
James Feist899e17f2019-09-13 11:46:29 -0700534
Patrick Williamsb7077432024-08-16 15:22:21 -0400535 inProgress = false;
James Feist2539ccd2020-05-01 16:15:08 -0700536
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200537 boost::asio::post(io, [this, newConfiguration, count] {
538 publishNewConfiguration(std::ref(instance), count,
539 std::ref(timer), newConfiguration);
540 });
Patrick Williamsb7077432024-08-16 15:22:21 -0400541 });
James Feist8f2710a2018-05-09 17:18:55 -0700542 perfScan->run();
543 });
James Feist75fdeeb2018-02-20 14:26:16 -0800544}
545
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600546// Check if InterfacesAdded payload contains an iface that needs probing.
Patrick Williamsb7077432024-08-16 15:22:21 -0400547static bool iaContainsProbeInterface(
548 sdbusplus::message_t& msg, const std::set<std::string>& probeInterfaces)
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600549{
550 sdbusplus::message::object_path path;
551 DBusObject interfaces;
552 std::set<std::string> interfaceSet;
553 std::set<std::string> intersect;
554
555 msg.read(path, interfaces);
556
557 std::for_each(interfaces.begin(), interfaces.end(),
558 [&interfaceSet](const auto& iface) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400559 interfaceSet.insert(iface.first);
560 });
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600561
562 std::set_intersection(interfaceSet.begin(), interfaceSet.end(),
563 probeInterfaces.begin(), probeInterfaces.end(),
564 std::inserter(intersect, intersect.end()));
565 return !intersect.empty();
566}
567
568// Check if InterfacesRemoved payload contains an iface that needs probing.
Patrick Williamsb7077432024-08-16 15:22:21 -0400569static bool irContainsProbeInterface(
570 sdbusplus::message_t& msg, const std::set<std::string>& probeInterfaces)
Matt Spinler8d1ac3a2023-02-02 09:48:04 -0600571{
572 sdbusplus::message::object_path path;
573 std::set<std::string> interfaces;
574 std::set<std::string> intersect;
575
576 msg.read(path, interfaces);
577
578 std::set_intersection(interfaces.begin(), interfaces.end(),
579 probeInterfaces.begin(), probeInterfaces.end(),
580 std::inserter(intersect, intersect.end()));
581 return !intersect.empty();
582}
583
Alexander Hansenad28e402025-06-25 12:38:20 +0200584void EntityManager::handleCurrentConfigurationJson()
585{
586 if (em_utils::fwVersionIsSame())
587 {
588 if (std::filesystem::is_regular_file(
589 configuration::currentConfiguration))
590 {
591 // this file could just be deleted, but it's nice for debug
592 std::filesystem::create_directory(tempConfigDir);
593 std::filesystem::remove(lastConfiguration);
594 std::filesystem::copy(configuration::currentConfiguration,
595 lastConfiguration);
596 std::filesystem::remove(configuration::currentConfiguration);
597
598 std::ifstream jsonStream(lastConfiguration);
599 if (jsonStream.good())
600 {
601 auto data = nlohmann::json::parse(jsonStream, nullptr, false);
602 if (data.is_discarded())
603 {
604 std::cerr
605 << "syntax error in " << lastConfiguration << "\n";
606 }
607 else
608 {
609 lastJson = std::move(data);
610 }
611 }
612 else
613 {
614 std::cerr << "unable to open " << lastConfiguration << "\n";
615 }
616 }
617 }
618 else
619 {
620 // not an error, just logging at this level to make it in the journal
621 std::cerr << "Clearing previous configuration\n";
622 std::filesystem::remove(configuration::currentConfiguration);
623 }
624}
625
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200626void EntityManager::registerCallback(const std::string& path)
James Feist75fdeeb2018-02-20 14:26:16 -0800627{
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200628 static boost::container::flat_map<std::string, sdbusplus::bus::match_t>
629 dbusMatches;
James Feist4131aea2018-03-09 09:47:30 -0800630
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200631 if (dbusMatches.contains(path))
632 {
633 return;
634 }
Nan Zhoua3315672022-09-20 19:48:14 +0000635
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200636 std::function<void(sdbusplus::message_t & message)> eventHandler =
637 [&](sdbusplus::message_t&) { propertiesChangedCallback(); };
James Feistfd1264a2018-05-03 12:10:00 -0700638
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200639 sdbusplus::bus::match_t match(
640 static_cast<sdbusplus::bus_t&>(*systemBus),
641 "type='signal',member='PropertiesChanged',path='" + path + "'",
642 eventHandler);
643 dbusMatches.emplace(path, std::move(match));
644}
James Feistfd1264a2018-05-03 12:10:00 -0700645
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200646// We need a poke from DBus for static providers that create all their
647// objects prior to claiming a well-known name, and thus don't emit any
648// org.freedesktop.DBus.Properties signals. Similarly if a process exits
649// for any reason, expected or otherwise, we'll need a poke to remove
650// entities from DBus.
651void EntityManager::initFilters(const std::set<std::string>& probeInterfaces)
652{
653 static sdbusplus::bus::match_t nameOwnerChangedMatch(
Patrick Williams2af39222022-07-22 19:26:56 -0500654 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishopc76af0f2020-12-04 13:50:23 -0500655 sdbusplus::bus::match::rules::nameOwnerChanged(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200656 [this](sdbusplus::message_t& m) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400657 auto [name, oldOwner,
658 newOwner] = m.unpack<std::string, std::string, std::string>();
Patrick Williams7b8786f2022-10-10 10:23:37 -0500659
Patrick Williamsb7077432024-08-16 15:22:21 -0400660 if (name.starts_with(':'))
661 {
662 // We should do nothing with unique-name connections.
663 return;
664 }
Patrick Williams7b8786f2022-10-10 10:23:37 -0500665
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200666 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400667 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200668
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500669 // We also need a poke from DBus when new interfaces are created or
670 // destroyed.
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200671 static sdbusplus::bus::match_t interfacesAddedMatch(
Patrick Williams2af39222022-07-22 19:26:56 -0500672 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500673 sdbusplus::bus::match::rules::interfacesAdded(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200674 [this, probeInterfaces](sdbusplus::message_t& msg) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400675 if (iaContainsProbeInterface(msg, probeInterfaces))
676 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200677 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400678 }
679 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200680
681 static sdbusplus::bus::match_t interfacesRemovedMatch(
Patrick Williams2af39222022-07-22 19:26:56 -0500682 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishop10a8c5f2020-12-07 21:40:07 -0500683 sdbusplus::bus::match::rules::interfacesRemoved(),
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200684 [this, probeInterfaces](sdbusplus::message_t& msg) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400685 if (irContainsProbeInterface(msg, probeInterfaces))
686 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200687 propertiesChangedCallback();
Patrick Williamsb7077432024-08-16 15:22:21 -0400688 }
689 });
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200690}
Brad Bishopc76af0f2020-12-04 13:50:23 -0500691
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200692int main()
693{
Alexander Hansena555acf2025-06-27 11:59:10 +0200694 boost::asio::io_context io;
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200695 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
696 systemBus->request_name("xyz.openbmc_project.EntityManager");
Alexander Hansena555acf2025-06-27 11:59:10 +0200697 EntityManager em(systemBus, io);
James Feist4131aea2018-03-09 09:47:30 -0800698
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200699 nlohmann::json systemConfiguration = nlohmann::json::object();
700
701 std::set<std::string> probeInterfaces = configuration::getProbeInterfaces();
702
703 em.initFilters(probeInterfaces);
704
705 boost::asio::post(io, [&]() { em.propertiesChangedCallback(); });
James Feist8f2710a2018-05-09 17:18:55 -0700706
Alexander Hansenad28e402025-06-25 12:38:20 +0200707 em.handleCurrentConfigurationJson();
James Feist1df06a42019-04-11 14:23:04 -0700708
709 // some boards only show up after power is on, we want to not say they are
710 // removed until the same state happens
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200711 em_utils::setupPowerMatch(em.systemBus);
James Feist1df06a42019-04-11 14:23:04 -0700712
James Feist1b2e2242018-01-30 13:45:19 -0800713 io.run();
James Feist3cb5fec2018-01-23 14:41:51 -0800714
715 return 0;
James Feist75fdeeb2018-02-20 14:26:16 -0800716}