blob: 96eb384b0279ca98a4b1f70ac415d08cd987468c [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
Brad Bishope45d8c72022-05-25 15:12:53 -040020#include "overlay.hpp"
Benjamin Fairca2eb042022-09-13 06:40:42 +000021#include "topology.hpp"
Brad Bishope45d8c72022-05-25 15:12:53 -040022#include "utils.hpp"
23#include "variant_visitors.hpp"
James Feist481c5d52019-08-13 14:40:40 -070024
James Feist11be6672018-04-06 14:05:32 -070025#include <boost/algorithm/string/case_conv.hpp>
James Feistf5125b02019-06-06 11:27:43 -070026#include <boost/algorithm/string/classification.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080027#include <boost/algorithm/string/predicate.hpp>
28#include <boost/algorithm/string/replace.hpp>
James Feistf5125b02019-06-06 11:27:43 -070029#include <boost/algorithm/string/split.hpp>
James Feist02d2b932020-02-06 16:28:48 -080030#include <boost/asio/io_context.hpp>
Ed Tanous49a888c2023-03-06 13:44:51 -080031#include <boost/asio/post.hpp>
James Feistb1728ca2020-04-30 15:40:55 -070032#include <boost/asio/steady_timer.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080033#include <boost/container/flat_map.hpp>
34#include <boost/container/flat_set.hpp>
James Feistf5125b02019-06-06 11:27:43 -070035#include <boost/range/iterator_range.hpp>
James Feist8c505da2020-05-28 10:06:33 -070036#include <nlohmann/json.hpp>
37#include <sdbusplus/asio/connection.hpp>
38#include <sdbusplus/asio/object_server.hpp>
39
Igor Kononenko9fd87e52020-10-06 01:18:17 +030040#include <charconv>
James Feist637b3ef2019-04-15 16:35:30 -070041#include <filesystem>
James Feista465ccc2019-02-08 12:51:01 -080042#include <fstream>
Andrew Jefferye35d0ac2022-03-24 15:53:13 +103043#include <functional>
James Feista465ccc2019-02-08 12:51:01 -080044#include <iostream>
Andrew Jeffery666583b2021-12-01 15:50:12 +103045#include <map>
James Feista465ccc2019-02-08 12:51:01 -080046#include <regex>
James Feista465ccc2019-02-08 12:51:01 -080047#include <variant>
Andrew Jefferya9c58922021-06-01 09:28:59 +093048constexpr const char* hostConfigurationDirectory = SYSCONF_DIR "configurations";
James Feista465ccc2019-02-08 12:51:01 -080049constexpr const char* configurationDirectory = PACKAGE_DIR "configurations";
50constexpr const char* schemaDirectory = PACKAGE_DIR "configurations/schemas";
James Feist1df06a42019-04-11 14:23:04 -070051constexpr const char* tempConfigDir = "/tmp/configuration/";
52constexpr const char* lastConfiguration = "/tmp/configuration/last.json";
53constexpr const char* currentConfiguration = "/var/configuration/system.json";
James Feista465ccc2019-02-08 12:51:01 -080054constexpr const char* globalSchema = "global.json";
James Feistf1b14142019-04-10 15:22:09 -070055
Andrew Jeffery666583b2021-12-01 15:50:12 +103056const boost::container::flat_map<const char*, probe_type_codes, CmpStr>
Ed Tanous07d467b2021-02-23 14:48:37 -080057 probeTypes{{{"FALSE", probe_type_codes::FALSE_T},
58 {"TRUE", probe_type_codes::TRUE_T},
59 {"AND", probe_type_codes::AND},
60 {"OR", probe_type_codes::OR},
61 {"FOUND", probe_type_codes::FOUND},
62 {"MATCH_ONE", probe_type_codes::MATCH_ONE}}};
James Feist3cb5fec2018-01-23 14:41:51 -080063
Adrian Ambrożewiczc789fca2020-05-14 15:50:05 +020064static constexpr std::array<const char*, 6> settableInterfaces = {
65 "FanProfile", "Pid", "Pid.Zone", "Stepwise", "Thresholds", "Polling"};
James Feist68500ff2018-08-08 15:40:42 -070066using JsonVariantType =
James Feist338b8a72019-03-01 10:16:45 -080067 std::variant<std::vector<std::string>, std::vector<double>, std::string,
68 int64_t, uint64_t, double, int32_t, uint32_t, int16_t,
69 uint16_t, uint8_t, bool>;
James Feist3cb5fec2018-01-23 14:41:51 -080070
Ed Tanousfc171422024-04-04 17:18:16 -070071// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
James Feistd58879a2019-09-11 11:26:07 -070072// store reference to all interfaces so we can destroy them later
73boost::container::flat_map<
James Feist02d2b932020-02-06 16:28:48 -080074 std::string, std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>>
James Feistd58879a2019-09-11 11:26:07 -070075 inventory;
76
James Feist3cb5fec2018-01-23 14:41:51 -080077// todo: pass this through nicer
Ed Tanous07d467b2021-02-23 14:48:37 -080078std::shared_ptr<sdbusplus::asio::connection> systemBus;
Andrew Jeffery47af65a2021-12-01 14:16:31 +103079nlohmann::json lastJson;
Matt Spinler6eb60972023-08-14 16:36:20 -050080Topology topology;
James Feist3cb5fec2018-01-23 14:41:51 -080081
James Feist02d2b932020-02-06 16:28:48 -080082boost::asio::io_context io;
Ed Tanousfc171422024-04-04 17:18:16 -070083// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)
James Feist02d2b932020-02-06 16:28:48 -080084
Ed Tanous07d467b2021-02-23 14:48:37 -080085const std::regex illegalDbusPathRegex("[^A-Za-z0-9_.]");
86const std::regex illegalDbusMemberRegex("[^A-Za-z0-9_]");
James Feist1b2e2242018-01-30 13:45:19 -080087
John Edward Broadbentd97c6312023-10-26 20:32:07 +000088void tryIfaceInitialize(std::shared_ptr<sdbusplus::asio::dbus_interface>& iface)
89{
90 try
91 {
92 iface->initialize();
93 }
94 catch (std::exception& e)
95 {
96 std::cerr << "Unable to initialize dbus interface : " << e.what()
97 << "\n"
98 << "object Path : " << iface->get_object_path() << "\n"
99 << "interface name : " << iface->get_interface_name() << "\n";
100 }
101}
102
Andrew Jeffery666583b2021-12-01 15:50:12 +1030103FoundProbeTypeT findProbeType(const std::string& probe)
104{
105 boost::container::flat_map<const char*, probe_type_codes,
106 CmpStr>::const_iterator probeType;
107 for (probeType = probeTypes.begin(); probeType != probeTypes.end();
108 ++probeType)
109 {
110 if (probe.find(probeType->first) != std::string::npos)
111 {
112 return probeType;
113 }
114 }
115
116 return std::nullopt;
117}
118
James Feistd58879a2019-09-11 11:26:07 -0700119static std::shared_ptr<sdbusplus::asio::dbus_interface>
120 createInterface(sdbusplus::asio::object_server& objServer,
121 const std::string& path, const std::string& interface,
James Feist02d2b932020-02-06 16:28:48 -0800122 const std::string& parent, bool checkNull = false)
James Feistd58879a2019-09-11 11:26:07 -0700123{
James Feist02d2b932020-02-06 16:28:48 -0800124 // on first add we have no reason to check for null before add, as there
125 // won't be any. For dynamically added interfaces, we check for null so that
126 // a constant delete/add will not create a memory leak
127
128 auto ptr = objServer.add_interface(path, interface);
129 auto& dataVector = inventory[parent];
130 if (checkNull)
131 {
132 auto it = std::find_if(dataVector.begin(), dataVector.end(),
133 [](const auto& p) { return p.expired(); });
134 if (it != dataVector.end())
135 {
136 *it = ptr;
137 return ptr;
138 }
139 }
140 dataVector.emplace_back(ptr);
141 return ptr;
James Feistd58879a2019-09-11 11:26:07 -0700142}
143
James Feist8f2710a2018-05-09 17:18:55 -0700144// writes output files to persist data
James Feista465ccc2019-02-08 12:51:01 -0800145bool writeJsonFiles(const nlohmann::json& systemConfiguration)
James Feist1b2e2242018-01-30 13:45:19 -0800146{
James Feist1df06a42019-04-11 14:23:04 -0700147 std::filesystem::create_directory(configurationOutDir);
148 std::ofstream output(currentConfiguration);
James Feistbb43d022018-06-12 15:44:33 -0700149 if (!output.good())
150 {
151 return false;
152 }
James Feist1b2e2242018-01-30 13:45:19 -0800153 output << systemConfiguration.dump(4);
154 output.close();
James Feistbb43d022018-06-12 15:44:33 -0700155 return true;
James Feist8f2710a2018-05-09 17:18:55 -0700156}
James Feist1b2e2242018-01-30 13:45:19 -0800157
James Feist97a63f12018-05-17 13:50:57 -0700158template <typename JsonType>
James Feista465ccc2019-02-08 12:51:01 -0800159bool setJsonFromPointer(const std::string& ptrStr, const JsonType& value,
160 nlohmann::json& systemConfiguration)
James Feist97a63f12018-05-17 13:50:57 -0700161{
162 try
163 {
164 nlohmann::json::json_pointer ptr(ptrStr);
James Feista465ccc2019-02-08 12:51:01 -0800165 nlohmann::json& ref = systemConfiguration[ptr];
James Feist97a63f12018-05-17 13:50:57 -0700166 ref = value;
167 return true;
168 }
James Feist98132792019-07-09 13:29:09 -0700169 catch (const std::out_of_range&)
James Feist97a63f12018-05-17 13:50:57 -0700170 {
171 return false;
172 }
173}
James Feistbb43d022018-06-12 15:44:33 -0700174
James Feistebcc26b2019-03-22 12:30:43 -0700175// template function to add array as dbus property
176template <typename PropertyType>
177void addArrayToDbus(const std::string& name, const nlohmann::json& array,
178 sdbusplus::asio::dbus_interface* iface,
179 sdbusplus::asio::PropertyPermission permission,
180 nlohmann::json& systemConfiguration,
181 const std::string& jsonPointerString)
182{
183 std::vector<PropertyType> values;
184 for (const auto& property : array)
185 {
186 auto ptr = property.get_ptr<const PropertyType*>();
187 if (ptr != nullptr)
188 {
189 values.emplace_back(*ptr);
190 }
191 }
192
193 if (permission == sdbusplus::asio::PropertyPermission::readOnly)
194 {
195 iface->register_property(name, values);
196 }
197 else
198 {
199 iface->register_property(
200 name, values,
201 [&systemConfiguration,
202 jsonPointerString{std::string(jsonPointerString)}](
203 const std::vector<PropertyType>& newVal,
204 std::vector<PropertyType>& val) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400205 val = newVal;
206 if (!setJsonFromPointer(jsonPointerString, val,
207 systemConfiguration))
208 {
209 std::cerr << "error setting json field\n";
210 return -1;
211 }
212 if (!writeJsonFiles(systemConfiguration))
213 {
214 std::cerr << "error setting json file\n";
215 return -1;
216 }
217 return 1;
218 });
James Feistebcc26b2019-03-22 12:30:43 -0700219 }
220}
221
James Feistbb43d022018-06-12 15:44:33 -0700222template <typename PropertyType>
Ed Tanous3013fb42022-07-09 08:27:06 -0700223void addProperty(const std::string& name, const PropertyType& value,
James Feista465ccc2019-02-08 12:51:01 -0800224 sdbusplus::asio::dbus_interface* iface,
225 nlohmann::json& systemConfiguration,
226 const std::string& jsonPointerString,
James Feistbb43d022018-06-12 15:44:33 -0700227 sdbusplus::asio::PropertyPermission permission)
228{
229 if (permission == sdbusplus::asio::PropertyPermission::readOnly)
230 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700231 iface->register_property(name, value);
James Feistbb43d022018-06-12 15:44:33 -0700232 return;
233 }
James Feist68500ff2018-08-08 15:40:42 -0700234 iface->register_property(
Ed Tanous3013fb42022-07-09 08:27:06 -0700235 name, value,
James Feist68500ff2018-08-08 15:40:42 -0700236 [&systemConfiguration,
237 jsonPointerString{std::string(jsonPointerString)}](
James Feista465ccc2019-02-08 12:51:01 -0800238 const PropertyType& newVal, PropertyType& val) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400239 val = newVal;
240 if (!setJsonFromPointer(jsonPointerString, val,
241 systemConfiguration))
242 {
243 std::cerr << "error setting json field\n";
244 return -1;
245 }
246 if (!writeJsonFiles(systemConfiguration))
247 {
248 std::cerr << "error setting json file\n";
249 return -1;
250 }
251 return 1;
252 });
James Feistc6248a52018-08-14 10:09:45 -0700253}
254
255void createDeleteObjectMethod(
James Feista465ccc2019-02-08 12:51:01 -0800256 const std::string& jsonPointerPath,
257 const std::shared_ptr<sdbusplus::asio::dbus_interface>& iface,
258 sdbusplus::asio::object_server& objServer,
259 nlohmann::json& systemConfiguration)
James Feistc6248a52018-08-14 10:09:45 -0700260{
261 std::weak_ptr<sdbusplus::asio::dbus_interface> interface = iface;
Patrick Williamsb7077432024-08-16 15:22:21 -0400262 iface->register_method(
263 "Delete", [&objServer, &systemConfiguration, interface,
264 jsonPointerPath{std::string(jsonPointerPath)}]() {
265 std::shared_ptr<sdbusplus::asio::dbus_interface> dbusInterface =
266 interface.lock();
267 if (!dbusInterface)
268 {
269 // this technically can't happen as the pointer is pointing to
270 // us
271 throw DBusInternalError();
272 }
273 nlohmann::json::json_pointer ptr(jsonPointerPath);
274 systemConfiguration[ptr] = nullptr;
James Feistc6248a52018-08-14 10:09:45 -0700275
Patrick Williamsb7077432024-08-16 15:22:21 -0400276 // todo(james): dig through sdbusplus to find out why we can't
277 // delete it in a method call
278 boost::asio::post(io, [&objServer, dbusInterface]() mutable {
279 objServer.remove_interface(dbusInterface);
280 });
281
282 if (!writeJsonFiles(systemConfiguration))
283 {
284 std::cerr << "error setting json file\n";
285 throw DBusInternalError();
286 }
James Feist68500ff2018-08-08 15:40:42 -0700287 });
James Feistbb43d022018-06-12 15:44:33 -0700288}
289
James Feist1b2e2242018-01-30 13:45:19 -0800290// adds simple json types to interface's properties
James Feistbb43d022018-06-12 15:44:33 -0700291void populateInterfaceFromJson(
James Feista465ccc2019-02-08 12:51:01 -0800292 nlohmann::json& systemConfiguration, const std::string& jsonPointerPath,
293 std::shared_ptr<sdbusplus::asio::dbus_interface>& iface,
294 nlohmann::json& dict, sdbusplus::asio::object_server& objServer,
James Feistbb43d022018-06-12 15:44:33 -0700295 sdbusplus::asio::PropertyPermission permission =
296 sdbusplus::asio::PropertyPermission::readOnly)
James Feist1b2e2242018-01-30 13:45:19 -0800297{
Patrick Williams2594d362022-09-28 06:46:24 -0500298 for (const auto& [key, value] : dict.items())
James Feist1b2e2242018-01-30 13:45:19 -0800299 {
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030300 auto type = value.type();
James Feist8f2710a2018-05-09 17:18:55 -0700301 bool array = false;
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030302 if (value.type() == nlohmann::json::value_t::array)
James Feist8f2710a2018-05-09 17:18:55 -0700303 {
304 array = true;
Ed Tanous3013fb42022-07-09 08:27:06 -0700305 if (value.empty())
James Feist8f2710a2018-05-09 17:18:55 -0700306 {
307 continue;
308 }
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030309 type = value[0].type();
James Feist8f2710a2018-05-09 17:18:55 -0700310 bool isLegal = true;
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030311 for (const auto& arrayItem : value)
James Feist8f2710a2018-05-09 17:18:55 -0700312 {
313 if (arrayItem.type() != type)
314 {
315 isLegal = false;
316 break;
317 }
318 }
319 if (!isLegal)
320 {
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030321 std::cerr << "dbus format error" << value << "\n";
James Feist8f2710a2018-05-09 17:18:55 -0700322 continue;
323 }
James Feista218ddb2019-04-11 14:01:31 -0700324 }
325 if (type == nlohmann::json::value_t::object)
326 {
327 continue; // handled elsewhere
James Feist8f2710a2018-05-09 17:18:55 -0700328 }
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030329
330 std::string path = jsonPointerPath;
331 path.append("/").append(key);
James Feistbb43d022018-06-12 15:44:33 -0700332 if (permission == sdbusplus::asio::PropertyPermission::readWrite)
333 {
334 // all setable numbers are doubles as it is difficult to always
335 // create a configuration file with all whole numbers as decimals
336 // i.e. 1.0
James Feistebcc26b2019-03-22 12:30:43 -0700337 if (array)
338 {
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030339 if (value[0].is_number())
James Feistebcc26b2019-03-22 12:30:43 -0700340 {
341 type = nlohmann::json::value_t::number_float;
342 }
343 }
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030344 else if (value.is_number())
James Feistbb43d022018-06-12 15:44:33 -0700345 {
346 type = nlohmann::json::value_t::number_float;
347 }
348 }
349
James Feist8f2710a2018-05-09 17:18:55 -0700350 switch (type)
James Feist1b2e2242018-01-30 13:45:19 -0800351 {
James Feist9eb0b582018-04-27 12:15:46 -0700352 case (nlohmann::json::value_t::boolean):
353 {
James Feist8f2710a2018-05-09 17:18:55 -0700354 if (array)
355 {
356 // todo: array of bool isn't detected correctly by
357 // sdbusplus, change it to numbers
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030358 addArrayToDbus<uint64_t>(key, value, iface.get(),
359 permission, systemConfiguration,
360 path);
James Feist8f2710a2018-05-09 17:18:55 -0700361 }
James Feistbb43d022018-06-12 15:44:33 -0700362
James Feist97a63f12018-05-17 13:50:57 -0700363 else
364 {
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030365 addProperty(key, value.get<bool>(), iface.get(),
366 systemConfiguration, path, permission);
James Feist97a63f12018-05-17 13:50:57 -0700367 }
James Feist9eb0b582018-04-27 12:15:46 -0700368 break;
369 }
370 case (nlohmann::json::value_t::number_integer):
371 {
James Feist8f2710a2018-05-09 17:18:55 -0700372 if (array)
373 {
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030374 addArrayToDbus<int64_t>(key, value, iface.get(), permission,
Andrew Jeffery029ee282022-03-25 13:11:36 +1030375 systemConfiguration, path);
James Feist97a63f12018-05-17 13:50:57 -0700376 }
377 else
378 {
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030379 addProperty(key, value.get<int64_t>(), iface.get(),
380 systemConfiguration, path,
James Feistbb43d022018-06-12 15:44:33 -0700381 sdbusplus::asio::PropertyPermission::readOnly);
James Feist97a63f12018-05-17 13:50:57 -0700382 }
James Feist9eb0b582018-04-27 12:15:46 -0700383 break;
384 }
385 case (nlohmann::json::value_t::number_unsigned):
386 {
James Feist8f2710a2018-05-09 17:18:55 -0700387 if (array)
388 {
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030389 addArrayToDbus<uint64_t>(key, value, iface.get(),
390 permission, systemConfiguration,
391 path);
James Feist97a63f12018-05-17 13:50:57 -0700392 }
393 else
394 {
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030395 addProperty(key, value.get<uint64_t>(), iface.get(),
Andrew Jeffery029ee282022-03-25 13:11:36 +1030396 systemConfiguration, path,
James Feistbb43d022018-06-12 15:44:33 -0700397 sdbusplus::asio::PropertyPermission::readOnly);
James Feist97a63f12018-05-17 13:50:57 -0700398 }
James Feist9eb0b582018-04-27 12:15:46 -0700399 break;
400 }
401 case (nlohmann::json::value_t::number_float):
402 {
James Feist8f2710a2018-05-09 17:18:55 -0700403 if (array)
404 {
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030405 addArrayToDbus<double>(key, value, iface.get(), permission,
Andrew Jeffery029ee282022-03-25 13:11:36 +1030406 systemConfiguration, path);
James Feist8f2710a2018-05-09 17:18:55 -0700407 }
James Feistbb43d022018-06-12 15:44:33 -0700408
James Feist97a63f12018-05-17 13:50:57 -0700409 else
410 {
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030411 addProperty(key, value.get<double>(), iface.get(),
412 systemConfiguration, path, permission);
James Feist97a63f12018-05-17 13:50:57 -0700413 }
James Feist9eb0b582018-04-27 12:15:46 -0700414 break;
415 }
416 case (nlohmann::json::value_t::string):
417 {
James Feist8f2710a2018-05-09 17:18:55 -0700418 if (array)
419 {
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030420 addArrayToDbus<std::string>(key, value, iface.get(),
421 permission, systemConfiguration,
422 path);
James Feist97a63f12018-05-17 13:50:57 -0700423 }
424 else
425 {
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030426 addProperty(key, value.get<std::string>(), iface.get(),
427 systemConfiguration, path, permission);
James Feist97a63f12018-05-17 13:50:57 -0700428 }
James Feist9eb0b582018-04-27 12:15:46 -0700429 break;
430 }
James Feist0eb40352019-04-09 14:44:04 -0700431 default:
432 {
James Feista218ddb2019-04-11 14:01:31 -0700433 std::cerr << "Unexpected json type in system configuration "
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030434 << key << ": " << value.type_name() << "\n";
James Feist0eb40352019-04-09 14:44:04 -0700435 break;
436 }
James Feist1b2e2242018-01-30 13:45:19 -0800437 }
438 }
James Feistc6248a52018-08-14 10:09:45 -0700439 if (permission == sdbusplus::asio::PropertyPermission::readWrite)
440 {
441 createDeleteObjectMethod(jsonPointerPath, iface, objServer,
442 systemConfiguration);
443 }
John Edward Broadbentd97c6312023-10-26 20:32:07 +0000444 tryIfaceInitialize(iface);
James Feist1b2e2242018-01-30 13:45:19 -0800445}
446
James Feista465ccc2019-02-08 12:51:01 -0800447sdbusplus::asio::PropertyPermission getPermission(const std::string& interface)
James Feistc6248a52018-08-14 10:09:45 -0700448{
449 return std::find(settableInterfaces.begin(), settableInterfaces.end(),
450 interface) != settableInterfaces.end()
451 ? sdbusplus::asio::PropertyPermission::readWrite
452 : sdbusplus::asio::PropertyPermission::readOnly;
453}
454
Patrick Williamsb7077432024-08-16 15:22:21 -0400455void createAddObjectMethod(
456 const std::string& jsonPointerPath, const std::string& path,
457 nlohmann::json& systemConfiguration,
458 sdbusplus::asio::object_server& objServer, const std::string& board)
James Feist68500ff2018-08-08 15:40:42 -0700459{
James Feistd58879a2019-09-11 11:26:07 -0700460 std::shared_ptr<sdbusplus::asio::dbus_interface> iface = createInterface(
461 objServer, path, "xyz.openbmc_project.AddObject", board);
James Feist68500ff2018-08-08 15:40:42 -0700462
463 iface->register_method(
464 "AddObject",
465 [&systemConfiguration, &objServer,
James Feistd58879a2019-09-11 11:26:07 -0700466 jsonPointerPath{std::string(jsonPointerPath)}, path{std::string(path)},
467 board](const boost::container::flat_map<std::string, JsonVariantType>&
468 data) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400469 nlohmann::json::json_pointer ptr(jsonPointerPath);
470 nlohmann::json& base = systemConfiguration[ptr];
471 auto findExposes = base.find("Exposes");
James Feist68500ff2018-08-08 15:40:42 -0700472
Patrick Williamsb7077432024-08-16 15:22:21 -0400473 if (findExposes == base.end())
James Feist68500ff2018-08-08 15:40:42 -0700474 {
Patrick Williamsb7077432024-08-16 15:22:21 -0400475 throw std::invalid_argument("Entity must have children.");
James Feist68500ff2018-08-08 15:40:42 -0700476 }
477
Patrick Williamsb7077432024-08-16 15:22:21 -0400478 // this will throw invalid-argument to sdbusplus if invalid json
479 nlohmann::json newData{};
480 for (const auto& item : data)
481 {
482 nlohmann::json& newJson = newData[item.first];
483 std::visit(
484 [&newJson](auto&& val) {
485 newJson = std::forward<decltype(val)>(val);
486 },
487 item.second);
488 }
489
490 auto findName = newData.find("Name");
491 auto findType = newData.find("Type");
492 if (findName == newData.end() || findType == newData.end())
493 {
494 throw std::invalid_argument("AddObject missing Name or Type");
495 }
496 const std::string* type = findType->get_ptr<const std::string*>();
497 const std::string* name = findName->get_ptr<const std::string*>();
498 if (type == nullptr || name == nullptr)
499 {
500 throw std::invalid_argument("Type and Name must be a string.");
501 }
502
503 bool foundNull = false;
504 size_t lastIndex = 0;
505 // we add in the "exposes"
506 for (const auto& expose : *findExposes)
507 {
508 if (expose.is_null())
509 {
510 foundNull = true;
511 continue;
512 }
513
514 if (expose["Name"] == *name && expose["Type"] == *type)
515 {
516 throw std::invalid_argument(
517 "Field already in JSON, not adding");
518 }
519
520 if (foundNull)
521 {
522 continue;
523 }
524
525 lastIndex++;
526 }
527
528 std::ifstream schemaFile(std::string(schemaDirectory) + "/" +
529 boost::to_lower_copy(*type) + ".json");
530 // todo(james) we might want to also make a list of 'can add'
531 // interfaces but for now I think the assumption if there is a
532 // schema avaliable that it is allowed to update is fine
533 if (!schemaFile.good())
James Feist68500ff2018-08-08 15:40:42 -0700534 {
535 throw std::invalid_argument(
Patrick Williamsb7077432024-08-16 15:22:21 -0400536 "No schema avaliable, cannot validate.");
James Feist68500ff2018-08-08 15:40:42 -0700537 }
Patrick Williamsb7077432024-08-16 15:22:21 -0400538 nlohmann::json schema =
539 nlohmann::json::parse(schemaFile, nullptr, false, true);
540 if (schema.is_discarded())
541 {
542 std::cerr << "Schema not legal" << *type << ".json\n";
543 throw DBusInternalError();
544 }
545 if (!validateJson(schema, newData))
546 {
547 throw std::invalid_argument("Data does not match schema");
548 }
James Feist02d2b932020-02-06 16:28:48 -0800549 if (foundNull)
550 {
Patrick Williamsb7077432024-08-16 15:22:21 -0400551 findExposes->at(lastIndex) = newData;
James Feist02d2b932020-02-06 16:28:48 -0800552 }
Patrick Williamsb7077432024-08-16 15:22:21 -0400553 else
554 {
555 findExposes->push_back(newData);
556 }
557 if (!writeJsonFiles(systemConfiguration))
558 {
559 std::cerr << "Error writing json files\n";
560 throw DBusInternalError();
561 }
562 std::string dbusName = *name;
James Feist68500ff2018-08-08 15:40:42 -0700563
Patrick Williamsb7077432024-08-16 15:22:21 -0400564 std::regex_replace(dbusName.begin(), dbusName.begin(),
565 dbusName.end(), illegalDbusMemberRegex, "_");
James Feistd58879a2019-09-11 11:26:07 -0700566
Patrick Williamsb7077432024-08-16 15:22:21 -0400567 std::shared_ptr<sdbusplus::asio::dbus_interface> interface =
568 createInterface(objServer, path + "/" + dbusName,
569 "xyz.openbmc_project.Configuration." + *type,
570 board, true);
571 // permission is read-write, as since we just created it, must be
572 // runtime modifiable
573 populateInterfaceFromJson(
574 systemConfiguration,
575 jsonPointerPath + "/Exposes/" + std::to_string(lastIndex),
576 interface, newData, objServer,
577 sdbusplus::asio::PropertyPermission::readWrite);
578 });
John Edward Broadbentd97c6312023-10-26 20:32:07 +0000579 tryIfaceInitialize(iface);
James Feist68500ff2018-08-08 15:40:42 -0700580}
581
James Feista465ccc2019-02-08 12:51:01 -0800582void postToDbus(const nlohmann::json& newConfiguration,
583 nlohmann::json& systemConfiguration,
584 sdbusplus::asio::object_server& objServer)
James Feist75fdeeb2018-02-20 14:26:16 -0800585
James Feist1b2e2242018-01-30 13:45:19 -0800586{
Matt Spinler6eb60972023-08-14 16:36:20 -0500587 std::map<std::string, std::string> newBoards; // path -> name
Benjamin Fairca2eb042022-09-13 06:40:42 +0000588
James Feist97a63f12018-05-17 13:50:57 -0700589 // iterate through boards
Patrick Williams2594d362022-09-28 06:46:24 -0500590 for (const auto& [boardId, boardConfig] : newConfiguration.items())
James Feist1b2e2242018-01-30 13:45:19 -0800591 {
Matt Spinler3d1909a2023-08-10 16:39:44 -0500592 std::string boardName = boardConfig["Name"];
593 std::string boardNameOrig = boardConfig["Name"];
Andrew Jeffery13132df2022-03-25 13:29:41 +1030594 std::string jsonPointerPath = "/" + boardId;
James Feist97a63f12018-05-17 13:50:57 -0700595 // loop through newConfiguration, but use values from system
596 // configuration to be able to modify via dbus later
Andrew Jeffery13132df2022-03-25 13:29:41 +1030597 auto boardValues = systemConfiguration[boardId];
James Feistd63d18a2018-07-19 15:23:45 -0700598 auto findBoardType = boardValues.find("Type");
James Feist1b2e2242018-01-30 13:45:19 -0800599 std::string boardType;
600 if (findBoardType != boardValues.end() &&
601 findBoardType->type() == nlohmann::json::value_t::string)
602 {
603 boardType = findBoardType->get<std::string>();
604 std::regex_replace(boardType.begin(), boardType.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800605 boardType.end(), illegalDbusMemberRegex, "_");
James Feist1b2e2242018-01-30 13:45:19 -0800606 }
607 else
608 {
Matt Spinler3d1909a2023-08-10 16:39:44 -0500609 std::cerr << "Unable to find type for " << boardName
James Feist1b2e2242018-01-30 13:45:19 -0800610 << " reverting to Chassis.\n";
611 boardType = "Chassis";
612 }
James Feist11be6672018-04-06 14:05:32 -0700613 std::string boardtypeLower = boost::algorithm::to_lower_copy(boardType);
James Feist1b2e2242018-01-30 13:45:19 -0800614
Matt Spinler3d1909a2023-08-10 16:39:44 -0500615 std::regex_replace(boardName.begin(), boardName.begin(),
616 boardName.end(), illegalDbusMemberRegex, "_");
617 std::string boardPath = "/xyz/openbmc_project/inventory/system/";
618 boardPath += boardtypeLower;
619 boardPath += "/";
620 boardPath += boardName;
James Feist1b2e2242018-01-30 13:45:19 -0800621
James Feistd58879a2019-09-11 11:26:07 -0700622 std::shared_ptr<sdbusplus::asio::dbus_interface> inventoryIface =
Matt Spinler3d1909a2023-08-10 16:39:44 -0500623 createInterface(objServer, boardPath,
624 "xyz.openbmc_project.Inventory.Item", boardName);
James Feist68500ff2018-08-08 15:40:42 -0700625
James Feistd58879a2019-09-11 11:26:07 -0700626 std::shared_ptr<sdbusplus::asio::dbus_interface> boardIface =
Matt Spinler3d1909a2023-08-10 16:39:44 -0500627 createInterface(objServer, boardPath,
James Feistd58879a2019-09-11 11:26:07 -0700628 "xyz.openbmc_project.Inventory.Item." + boardType,
Matt Spinler3d1909a2023-08-10 16:39:44 -0500629 boardNameOrig);
James Feist11be6672018-04-06 14:05:32 -0700630
Matt Spinler3d1909a2023-08-10 16:39:44 -0500631 createAddObjectMethod(jsonPointerPath, boardPath, systemConfiguration,
632 objServer, boardNameOrig);
James Feist68500ff2018-08-08 15:40:42 -0700633
James Feist97a63f12018-05-17 13:50:57 -0700634 populateInterfaceFromJson(systemConfiguration, jsonPointerPath,
James Feistc6248a52018-08-14 10:09:45 -0700635 boardIface, boardValues, objServer);
James Feist97a63f12018-05-17 13:50:57 -0700636 jsonPointerPath += "/";
637 // iterate through board properties
Patrick Williams2594d362022-09-28 06:46:24 -0500638 for (const auto& [propName, propValue] : boardValues.items())
James Feist11be6672018-04-06 14:05:32 -0700639 {
Andrew Jefferya96950d2022-03-25 13:32:46 +1030640 if (propValue.type() == nlohmann::json::value_t::object)
James Feist11be6672018-04-06 14:05:32 -0700641 {
James Feistd58879a2019-09-11 11:26:07 -0700642 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
Matt Spinler3d1909a2023-08-10 16:39:44 -0500643 createInterface(objServer, boardPath, propName,
644 boardNameOrig);
James Feistd58879a2019-09-11 11:26:07 -0700645
James Feistc6248a52018-08-14 10:09:45 -0700646 populateInterfaceFromJson(systemConfiguration,
Andrew Jefferya96950d2022-03-25 13:32:46 +1030647 jsonPointerPath + propName, iface,
648 propValue, objServer);
James Feist11be6672018-04-06 14:05:32 -0700649 }
650 }
James Feist97a63f12018-05-17 13:50:57 -0700651
James Feist1e3e6982018-08-03 16:09:28 -0700652 auto exposes = boardValues.find("Exposes");
James Feist1b2e2242018-01-30 13:45:19 -0800653 if (exposes == boardValues.end())
654 {
655 continue;
656 }
James Feist97a63f12018-05-17 13:50:57 -0700657 // iterate through exposes
James Feist1e3e6982018-08-03 16:09:28 -0700658 jsonPointerPath += "Exposes/";
James Feist97a63f12018-05-17 13:50:57 -0700659
660 // store the board level pointer so we can modify it on the way down
661 std::string jsonPointerPathBoard = jsonPointerPath;
662 size_t exposesIndex = -1;
James Feista465ccc2019-02-08 12:51:01 -0800663 for (auto& item : *exposes)
James Feist1b2e2242018-01-30 13:45:19 -0800664 {
James Feist97a63f12018-05-17 13:50:57 -0700665 exposesIndex++;
666 jsonPointerPath = jsonPointerPathBoard;
667 jsonPointerPath += std::to_string(exposesIndex);
668
James Feistd63d18a2018-07-19 15:23:45 -0700669 auto findName = item.find("Name");
James Feist1b2e2242018-01-30 13:45:19 -0800670 if (findName == item.end())
671 {
672 std::cerr << "cannot find name in field " << item << "\n";
673 continue;
674 }
James Feist1e3e6982018-08-03 16:09:28 -0700675 auto findStatus = item.find("Status");
James Feist1b2e2242018-01-30 13:45:19 -0800676 // if status is not found it is assumed to be status = 'okay'
677 if (findStatus != item.end())
678 {
679 if (*findStatus == "disabled")
680 {
681 continue;
682 }
683 }
James Feistd63d18a2018-07-19 15:23:45 -0700684 auto findType = item.find("Type");
James Feist1b2e2242018-01-30 13:45:19 -0800685 std::string itemType;
686 if (findType != item.end())
687 {
688 itemType = findType->get<std::string>();
689 std::regex_replace(itemType.begin(), itemType.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800690 itemType.end(), illegalDbusPathRegex, "_");
James Feist1b2e2242018-01-30 13:45:19 -0800691 }
692 else
693 {
694 itemType = "unknown";
695 }
696 std::string itemName = findName->get<std::string>();
697 std::regex_replace(itemName.begin(), itemName.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800698 itemName.end(), illegalDbusMemberRegex, "_");
Matt Spinler3d1909a2023-08-10 16:39:44 -0500699 std::string ifacePath = boardPath;
Ed Tanous07d467b2021-02-23 14:48:37 -0800700 ifacePath += "/";
701 ifacePath += itemName;
James Feistc6248a52018-08-14 10:09:45 -0700702
Sui Chen74ebe592022-09-13 10:22:03 -0700703 if (itemType == "BMC")
704 {
705 std::shared_ptr<sdbusplus::asio::dbus_interface> bmcIface =
706 createInterface(objServer, ifacePath,
707 "xyz.openbmc_project.Inventory.Item.Bmc",
Matt Spinler3d1909a2023-08-10 16:39:44 -0500708 boardNameOrig);
Sui Chen74ebe592022-09-13 10:22:03 -0700709 populateInterfaceFromJson(systemConfiguration, jsonPointerPath,
710 bmcIface, item, objServer,
711 getPermission(itemType));
712 }
Edward Leeeb587b42023-03-08 18:59:04 +0000713 else if (itemType == "System")
714 {
715 std::shared_ptr<sdbusplus::asio::dbus_interface> systemIface =
716 createInterface(objServer, ifacePath,
717 "xyz.openbmc_project.Inventory.Item.System",
Matt Spinler3d1909a2023-08-10 16:39:44 -0500718 boardNameOrig);
Edward Leeeb587b42023-03-08 18:59:04 +0000719 populateInterfaceFromJson(systemConfiguration, jsonPointerPath,
720 systemIface, item, objServer,
721 getPermission(itemType));
722 }
Sui Chen74ebe592022-09-13 10:22:03 -0700723
Patrick Williams2594d362022-09-28 06:46:24 -0500724 for (const auto& [name, config] : item.items())
James Feist1b2e2242018-01-30 13:45:19 -0800725 {
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030726 jsonPointerPath = jsonPointerPathBoard;
727 jsonPointerPath.append(std::to_string(exposesIndex))
728 .append("/")
729 .append(name);
730 if (config.type() == nlohmann::json::value_t::object)
James Feist1b2e2242018-01-30 13:45:19 -0800731 {
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030732 std::string ifaceName =
733 "xyz.openbmc_project.Configuration.";
734 ifaceName.append(itemType).append(".").append(name);
James Feist97a63f12018-05-17 13:50:57 -0700735
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030736 std::shared_ptr<sdbusplus::asio::dbus_interface>
737 objectIface = createInterface(objServer, ifacePath,
Matt Spinler3d1909a2023-08-10 16:39:44 -0500738 ifaceName, boardNameOrig);
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030739
740 populateInterfaceFromJson(
741 systemConfiguration, jsonPointerPath, objectIface,
742 config, objServer, getPermission(name));
James Feist1b2e2242018-01-30 13:45:19 -0800743 }
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030744 else if (config.type() == nlohmann::json::value_t::array)
James Feist1b2e2242018-01-30 13:45:19 -0800745 {
746 size_t index = 0;
Ed Tanous3013fb42022-07-09 08:27:06 -0700747 if (config.empty())
James Feist1b2e2242018-01-30 13:45:19 -0800748 {
James Feist8f2710a2018-05-09 17:18:55 -0700749 continue;
750 }
751 bool isLegal = true;
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030752 auto type = config[0].type();
James Feist8f2710a2018-05-09 17:18:55 -0700753 if (type != nlohmann::json::value_t::object)
754 {
755 continue;
756 }
757
758 // verify legal json
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030759 for (const auto& arrayItem : config)
James Feist8f2710a2018-05-09 17:18:55 -0700760 {
761 if (arrayItem.type() != type)
James Feist1b2e2242018-01-30 13:45:19 -0800762 {
James Feist8f2710a2018-05-09 17:18:55 -0700763 isLegal = false;
James Feist1b2e2242018-01-30 13:45:19 -0800764 break;
765 }
James Feist8f2710a2018-05-09 17:18:55 -0700766 }
767 if (!isLegal)
768 {
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030769 std::cerr << "dbus format error" << config << "\n";
James Feist8f2710a2018-05-09 17:18:55 -0700770 break;
771 }
772
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030773 for (auto& arrayItem : config)
James Feist8f2710a2018-05-09 17:18:55 -0700774 {
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030775 std::string ifaceName =
776 "xyz.openbmc_project.Configuration.";
777 ifaceName.append(itemType).append(".").append(name);
778 ifaceName.append(std::to_string(index));
James Feist97a63f12018-05-17 13:50:57 -0700779
James Feistd58879a2019-09-11 11:26:07 -0700780 std::shared_ptr<sdbusplus::asio::dbus_interface>
781 objectIface = createInterface(
Matt Spinler3d1909a2023-08-10 16:39:44 -0500782 objServer, ifacePath, ifaceName, boardNameOrig);
James Feistd58879a2019-09-11 11:26:07 -0700783
James Feistc6248a52018-08-14 10:09:45 -0700784 populateInterfaceFromJson(
785 systemConfiguration,
786 jsonPointerPath + "/" + std::to_string(index),
787 objectIface, arrayItem, objServer,
Andrew Jeffery5a6379c2022-03-25 13:25:31 +1030788 getPermission(name));
James Feistbb43d022018-06-12 15:44:33 -0700789 index++;
James Feist1b2e2242018-01-30 13:45:19 -0800790 }
791 }
792 }
Benjamin Fairca2eb042022-09-13 06:40:42 +0000793
George Liu5c1a61a2024-10-24 18:02:29 +0800794 std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface =
795 createInterface(objServer, ifacePath,
796 "xyz.openbmc_project.Configuration." + itemType,
797 boardNameOrig);
798
799 populateInterfaceFromJson(systemConfiguration, jsonPointerPath,
800 itemIface, item, objServer,
801 getPermission(itemType));
802
Matt Spinler6eb60972023-08-14 16:36:20 -0500803 topology.addBoard(boardPath, boardType, boardNameOrig, item);
James Feist1b2e2242018-01-30 13:45:19 -0800804 }
Matt Spinler6eb60972023-08-14 16:36:20 -0500805
806 newBoards.emplace(boardPath, boardNameOrig);
James Feist1b2e2242018-01-30 13:45:19 -0800807 }
Benjamin Fairca2eb042022-09-13 06:40:42 +0000808
Matt Spinler6eb60972023-08-14 16:36:20 -0500809 for (const auto& [assocPath, assocPropValue] :
810 topology.getAssocs(newBoards))
Benjamin Fairca2eb042022-09-13 06:40:42 +0000811 {
Matt Spinler6eb60972023-08-14 16:36:20 -0500812 auto findBoard = newBoards.find(assocPath);
813 if (findBoard == newBoards.end())
814 {
815 continue;
816 }
Benjamin Fairca2eb042022-09-13 06:40:42 +0000817
Matt Spinler6eb60972023-08-14 16:36:20 -0500818 auto ifacePtr = createInterface(
819 objServer, assocPath, "xyz.openbmc_project.Association.Definitions",
820 findBoard->second);
821
822 ifacePtr->register_property("Associations", assocPropValue);
John Edward Broadbentd97c6312023-10-26 20:32:07 +0000823 tryIfaceInitialize(ifacePtr);
Benjamin Fairca2eb042022-09-13 06:40:42 +0000824 }
James Feist1b2e2242018-01-30 13:45:19 -0800825}
826
James Feist8f2710a2018-05-09 17:18:55 -0700827// reads json files out of the filesystem
Andrew Jefferyf3311792022-03-29 22:09:00 +1030828bool loadConfigurations(std::list<nlohmann::json>& configurations)
James Feist3cb5fec2018-01-23 14:41:51 -0800829{
830 // find configuration files
Ed Tanous072e25d2018-12-16 21:45:20 -0800831 std::vector<std::filesystem::path> jsonPaths;
Andrew Jefferya9c58922021-06-01 09:28:59 +0930832 if (!findFiles(
833 std::vector<std::filesystem::path>{configurationDirectory,
834 hostConfigurationDirectory},
835 R"(.*\.json)", jsonPaths))
James Feist3cb5fec2018-01-23 14:41:51 -0800836 {
837 std::cerr << "Unable to find any configuration files in "
James Feistb4383f42018-08-06 16:54:10 -0700838 << configurationDirectory << "\n";
James Feist75fdeeb2018-02-20 14:26:16 -0800839 return false;
James Feist3cb5fec2018-01-23 14:41:51 -0800840 }
James Feistb4383f42018-08-06 16:54:10 -0700841
Patrick Williamsb7077432024-08-16 15:22:21 -0400842 std::ifstream schemaStream(
843 std::string(schemaDirectory) + "/" + globalSchema);
James Feistb4383f42018-08-06 16:54:10 -0700844 if (!schemaStream.good())
845 {
846 std::cerr
847 << "Cannot open schema file, cannot validate JSON, exiting\n\n";
848 std::exit(EXIT_FAILURE);
Ed Tanous072e25d2018-12-16 21:45:20 -0800849 return false;
James Feistb4383f42018-08-06 16:54:10 -0700850 }
Patrick Williamsb7077432024-08-16 15:22:21 -0400851 nlohmann::json schema =
852 nlohmann::json::parse(schemaStream, nullptr, false, true);
James Feistb4383f42018-08-06 16:54:10 -0700853 if (schema.is_discarded())
854 {
855 std::cerr
856 << "Illegal schema file detected, cannot validate JSON, exiting\n";
857 std::exit(EXIT_FAILURE);
Ed Tanous072e25d2018-12-16 21:45:20 -0800858 return false;
James Feistb4383f42018-08-06 16:54:10 -0700859 }
860
James Feista465ccc2019-02-08 12:51:01 -0800861 for (auto& jsonPath : jsonPaths)
James Feist3cb5fec2018-01-23 14:41:51 -0800862 {
863 std::ifstream jsonStream(jsonPath.c_str());
864 if (!jsonStream.good())
865 {
866 std::cerr << "unable to open " << jsonPath.string() << "\n";
867 continue;
868 }
Potin Lai0f3a4d92023-12-05 00:13:55 +0800869 auto data = nlohmann::json::parse(jsonStream, nullptr, false, true);
James Feist3cb5fec2018-01-23 14:41:51 -0800870 if (data.is_discarded())
871 {
872 std::cerr << "syntax error in " << jsonPath.string() << "\n";
873 continue;
874 }
James Feist8da99192019-01-24 08:20:16 -0800875 /*
876 * todo(james): reenable this once less things are in flight
877 *
James Feistb4383f42018-08-06 16:54:10 -0700878 if (!validateJson(schema, data))
879 {
880 std::cerr << "Error validating " << jsonPath.string() << "\n";
881 continue;
882 }
James Feist8da99192019-01-24 08:20:16 -0800883 */
James Feistb4383f42018-08-06 16:54:10 -0700884
James Feist3cb5fec2018-01-23 14:41:51 -0800885 if (data.type() == nlohmann::json::value_t::array)
886 {
James Feista465ccc2019-02-08 12:51:01 -0800887 for (auto& d : data)
James Feist3cb5fec2018-01-23 14:41:51 -0800888 {
889 configurations.emplace_back(d);
890 }
891 }
892 else
893 {
894 configurations.emplace_back(data);
895 }
896 }
Ed Tanous072e25d2018-12-16 21:45:20 -0800897 return true;
James Feist75fdeeb2018-02-20 14:26:16 -0800898}
James Feist3cb5fec2018-01-23 14:41:51 -0800899
Andrew Jeffery55192932022-03-24 12:29:27 +1030900static bool deviceRequiresPowerOn(const nlohmann::json& entity)
901{
902 auto powerState = entity.find("PowerState");
Andrew Jefferyb6209442022-03-24 12:36:20 +1030903 if (powerState == entity.end())
Andrew Jeffery55192932022-03-24 12:29:27 +1030904 {
Andrew Jefferyb6209442022-03-24 12:36:20 +1030905 return false;
Andrew Jeffery55192932022-03-24 12:29:27 +1030906 }
907
Ed Tanous3013fb42022-07-09 08:27:06 -0700908 const auto* ptr = powerState->get_ptr<const std::string*>();
909 if (ptr == nullptr)
Andrew Jefferyb6209442022-03-24 12:36:20 +1030910 {
911 return false;
912 }
913
914 return *ptr == "On" || *ptr == "BiosPost";
Andrew Jeffery55192932022-03-24 12:29:27 +1030915}
916
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030917static void pruneDevice(const nlohmann::json& systemConfiguration,
918 const bool powerOff, const bool scannedPowerOff,
919 const std::string& name, const nlohmann::json& device)
920{
921 if (systemConfiguration.contains(name))
922 {
923 return;
924 }
925
Andrew Jeffery4db38bc2022-03-24 13:42:41 +1030926 if (deviceRequiresPowerOn(device) && (powerOff || scannedPowerOff))
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030927 {
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030928 return;
929 }
930
931 logDeviceRemoved(device);
932}
933
James Feistb1728ca2020-04-30 15:40:55 -0700934void startRemovedTimer(boost::asio::steady_timer& timer,
James Feist1df06a42019-04-11 14:23:04 -0700935 nlohmann::json& systemConfiguration)
936{
937 static bool scannedPowerOff = false;
938 static bool scannedPowerOn = false;
939
James Feistfb00f392019-06-25 14:16:48 -0700940 if (systemConfiguration.empty() || lastJson.empty())
941 {
942 return; // not ready yet
943 }
James Feist1df06a42019-04-11 14:23:04 -0700944 if (scannedPowerOn)
945 {
946 return;
947 }
948
949 if (!isPowerOn() && scannedPowerOff)
950 {
951 return;
952 }
953
James Feistb1728ca2020-04-30 15:40:55 -0700954 timer.expires_after(std::chrono::seconds(10));
Andrew Jeffery27a1cfb2022-03-24 12:31:53 +1030955 timer.async_wait(
956 [&systemConfiguration](const boost::system::error_code& ec) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400957 if (ec == boost::asio::error::operation_aborted)
958 {
959 return;
960 }
Andrew Jeffery27a1cfb2022-03-24 12:31:53 +1030961
Patrick Williamsb7077432024-08-16 15:22:21 -0400962 bool powerOff = !isPowerOn();
963 for (const auto& [name, device] : lastJson.items())
964 {
965 pruneDevice(systemConfiguration, powerOff, scannedPowerOff,
966 name, device);
967 }
Andrew Jeffery89ec3522022-03-24 13:30:41 +1030968
Patrick Williamsb7077432024-08-16 15:22:21 -0400969 scannedPowerOff = true;
970 if (!powerOff)
971 {
972 scannedPowerOn = true;
973 }
974 });
James Feist1df06a42019-04-11 14:23:04 -0700975}
976
Andrew Jeffery2f750d22022-03-24 14:32:57 +1030977static std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>&
978 getDeviceInterfaces(const nlohmann::json& device)
979{
980 return inventory[device["Name"].get<std::string>()];
981}
982
Andrew Jeffery0d0c1462022-03-24 15:26:39 +1030983static void pruneConfiguration(nlohmann::json& systemConfiguration,
984 sdbusplus::asio::object_server& objServer,
985 bool powerOff, const std::string& name,
986 const nlohmann::json& device)
987{
988 if (powerOff && deviceRequiresPowerOn(device))
989 {
990 // power not on yet, don't know if it's there or not
991 return;
992 }
993
994 auto& ifaces = getDeviceInterfaces(device);
995 for (auto& iface : ifaces)
996 {
997 auto sharedPtr = iface.lock();
998 if (!!sharedPtr)
999 {
1000 objServer.remove_interface(sharedPtr);
1001 }
1002 }
1003
1004 ifaces.clear();
1005 systemConfiguration.erase(name);
Matt Spinler6eb60972023-08-14 16:36:20 -05001006 topology.remove(device["Name"].get<std::string>());
Andrew Jeffery0d0c1462022-03-24 15:26:39 +10301007 logDeviceRemoved(device);
1008}
1009
Andrew Jeffery7c4cbbe2022-03-24 15:27:10 +10301010static void deriveNewConfiguration(const nlohmann::json& oldConfiguration,
1011 nlohmann::json& newConfiguration)
1012{
1013 for (auto it = newConfiguration.begin(); it != newConfiguration.end();)
1014 {
1015 auto findKey = oldConfiguration.find(it.key());
1016 if (findKey != oldConfiguration.end())
1017 {
1018 it = newConfiguration.erase(it);
1019 }
1020 else
1021 {
1022 it++;
1023 }
1024 }
1025}
1026
Andrew Jefferye35d0ac2022-03-24 15:53:13 +10301027static void publishNewConfiguration(
1028 const size_t& instance, const size_t count,
1029 boost::asio::steady_timer& timer, nlohmann::json& systemConfiguration,
1030 // Gerrit discussion:
1031 // https://gerrit.openbmc-project.xyz/c/openbmc/entity-manager/+/52316/6
1032 //
1033 // Discord discussion:
1034 // https://discord.com/channels/775381525260664832/867820390406422538/958048437729910854
1035 //
1036 // NOLINTNEXTLINE(performance-unnecessary-value-param)
1037 const nlohmann::json newConfiguration,
1038 sdbusplus::asio::object_server& objServer)
1039{
1040 loadOverlays(newConfiguration);
1041
Ed Tanous49a888c2023-03-06 13:44:51 -08001042 boost::asio::post(io, [systemConfiguration]() {
Andrew Jefferye35d0ac2022-03-24 15:53:13 +10301043 if (!writeJsonFiles(systemConfiguration))
1044 {
1045 std::cerr << "Error writing json files\n";
1046 }
1047 });
1048
Ed Tanous49a888c2023-03-06 13:44:51 -08001049 boost::asio::post(io, [&instance, count, &timer, newConfiguration,
1050 &systemConfiguration, &objServer]() {
Andrew Jefferye35d0ac2022-03-24 15:53:13 +10301051 postToDbus(newConfiguration, systemConfiguration, objServer);
1052 if (count == instance)
1053 {
1054 startRemovedTimer(timer, systemConfiguration);
1055 }
1056 });
1057}
1058
James Feist8f2710a2018-05-09 17:18:55 -07001059// main properties changed entry
James Feist4dc617b2020-05-01 09:54:47 -07001060void propertiesChangedCallback(nlohmann::json& systemConfiguration,
1061 sdbusplus::asio::object_server& objServer)
James Feist8f2710a2018-05-09 17:18:55 -07001062{
James Feist2539ccd2020-05-01 16:15:08 -07001063 static bool inProgress = false;
James Feistb1728ca2020-04-30 15:40:55 -07001064 static boost::asio::steady_timer timer(io);
James Feist899e17f2019-09-13 11:46:29 -07001065 static size_t instance = 0;
1066 instance++;
1067 size_t count = instance;
James Feist1df06a42019-04-11 14:23:04 -07001068
Anupama B Rbf263982024-01-25 04:42:39 -06001069 timer.expires_after(std::chrono::milliseconds(500));
James Feist8f2710a2018-05-09 17:18:55 -07001070
1071 // setup an async wait as we normally get flooded with new requests
James Feist4dc617b2020-05-01 09:54:47 -07001072 timer.async_wait([&systemConfiguration, &objServer,
James Feist899e17f2019-09-13 11:46:29 -07001073 count](const boost::system::error_code& ec) {
James Feist8f2710a2018-05-09 17:18:55 -07001074 if (ec == boost::asio::error::operation_aborted)
1075 {
1076 // we were cancelled
1077 return;
1078 }
Ed Tanous07d467b2021-02-23 14:48:37 -08001079 if (ec)
James Feist8f2710a2018-05-09 17:18:55 -07001080 {
1081 std::cerr << "async wait error " << ec << "\n";
1082 return;
1083 }
1084
James Feist2539ccd2020-05-01 16:15:08 -07001085 if (inProgress)
1086 {
1087 propertiesChangedCallback(systemConfiguration, objServer);
1088 return;
1089 }
1090 inProgress = true;
1091
James Feist8f2710a2018-05-09 17:18:55 -07001092 nlohmann::json oldConfiguration = systemConfiguration;
James Feist899e17f2019-09-13 11:46:29 -07001093 auto missingConfigurations = std::make_shared<nlohmann::json>();
1094 *missingConfigurations = systemConfiguration;
1095
James Feist8f2710a2018-05-09 17:18:55 -07001096 std::list<nlohmann::json> configurations;
Andrew Jefferyf3311792022-03-29 22:09:00 +10301097 if (!loadConfigurations(configurations))
James Feist8f2710a2018-05-09 17:18:55 -07001098 {
Andrew Jefferyf3311792022-03-29 22:09:00 +10301099 std::cerr << "Could not load configurations\n";
James Feist2539ccd2020-05-01 16:15:08 -07001100 inProgress = false;
James Feist8f2710a2018-05-09 17:18:55 -07001101 return;
1102 }
1103
1104 auto perfScan = std::make_shared<PerformScan>(
James Feist899e17f2019-09-13 11:46:29 -07001105 systemConfiguration, *missingConfigurations, configurations,
James Feist4dc617b2020-05-01 09:54:47 -07001106 objServer,
1107 [&systemConfiguration, &objServer, count, oldConfiguration,
1108 missingConfigurations]() {
Patrick Williamsb7077432024-08-16 15:22:21 -04001109 // this is something that since ac has been applied to the bmc
1110 // we saw, and we no longer see it
1111 bool powerOff = !isPowerOn();
1112 for (const auto& [name, device] :
1113 missingConfigurations->items())
1114 {
1115 pruneConfiguration(systemConfiguration, objServer, powerOff,
1116 name, device);
1117 }
James Feist899e17f2019-09-13 11:46:29 -07001118
Patrick Williamsb7077432024-08-16 15:22:21 -04001119 nlohmann::json newConfiguration = systemConfiguration;
Andrew Jeffery7c4cbbe2022-03-24 15:27:10 +10301120
Patrick Williamsb7077432024-08-16 15:22:21 -04001121 deriveNewConfiguration(oldConfiguration, newConfiguration);
Andrew Jeffery7c4cbbe2022-03-24 15:27:10 +10301122
Patrick Williamsb7077432024-08-16 15:22:21 -04001123 for (const auto& [_, device] : newConfiguration.items())
1124 {
1125 logDeviceAdded(device);
1126 }
James Feist899e17f2019-09-13 11:46:29 -07001127
Patrick Williamsb7077432024-08-16 15:22:21 -04001128 inProgress = false;
James Feist2539ccd2020-05-01 16:15:08 -07001129
Patrick Williamsb7077432024-08-16 15:22:21 -04001130 boost::asio::post(
1131 io, std::bind_front(
1132 publishNewConfiguration, std::ref(instance), count,
1133 std::ref(timer), std::ref(systemConfiguration),
1134 newConfiguration, std::ref(objServer)));
1135 });
James Feist8f2710a2018-05-09 17:18:55 -07001136 perfScan->run();
1137 });
James Feist75fdeeb2018-02-20 14:26:16 -08001138}
1139
Matt Spinler8d1ac3a2023-02-02 09:48:04 -06001140// Extract the D-Bus interfaces to probe from the JSON config files.
1141static std::set<std::string> getProbeInterfaces()
1142{
1143 std::set<std::string> interfaces;
1144 std::list<nlohmann::json> configurations;
1145 if (!loadConfigurations(configurations))
1146 {
1147 return interfaces;
1148 }
1149
1150 for (auto it = configurations.begin(); it != configurations.end();)
1151 {
1152 auto findProbe = it->find("Probe");
1153 if (findProbe == it->end())
1154 {
1155 std::cerr << "configuration file missing probe:\n " << *it << "\n";
1156 it++;
1157 continue;
1158 }
1159
1160 nlohmann::json probeCommand;
1161 if ((*findProbe).type() != nlohmann::json::value_t::array)
1162 {
1163 probeCommand = nlohmann::json::array();
1164 probeCommand.push_back(*findProbe);
1165 }
1166 else
1167 {
1168 probeCommand = *findProbe;
1169 }
1170
1171 for (const nlohmann::json& probeJson : probeCommand)
1172 {
1173 const std::string* probe = probeJson.get_ptr<const std::string*>();
1174 if (probe == nullptr)
1175 {
1176 std::cerr << "Probe statement wasn't a string, can't parse";
1177 continue;
1178 }
1179 // Skip it if the probe cmd doesn't contain an interface.
1180 if (findProbeType(*probe))
1181 {
1182 continue;
1183 }
1184
1185 // syntax requires probe before first open brace
1186 auto findStart = probe->find('(');
1187 if (findStart != std::string::npos)
1188 {
1189 std::string interface = probe->substr(0, findStart);
1190 interfaces.emplace(interface);
1191 }
1192 }
1193 it++;
1194 }
1195
1196 return interfaces;
1197}
1198
1199// Check if InterfacesAdded payload contains an iface that needs probing.
Patrick Williamsb7077432024-08-16 15:22:21 -04001200static bool iaContainsProbeInterface(
1201 sdbusplus::message_t& msg, const std::set<std::string>& probeInterfaces)
Matt Spinler8d1ac3a2023-02-02 09:48:04 -06001202{
1203 sdbusplus::message::object_path path;
1204 DBusObject interfaces;
1205 std::set<std::string> interfaceSet;
1206 std::set<std::string> intersect;
1207
1208 msg.read(path, interfaces);
1209
1210 std::for_each(interfaces.begin(), interfaces.end(),
1211 [&interfaceSet](const auto& iface) {
Patrick Williamsb7077432024-08-16 15:22:21 -04001212 interfaceSet.insert(iface.first);
1213 });
Matt Spinler8d1ac3a2023-02-02 09:48:04 -06001214
1215 std::set_intersection(interfaceSet.begin(), interfaceSet.end(),
1216 probeInterfaces.begin(), probeInterfaces.end(),
1217 std::inserter(intersect, intersect.end()));
1218 return !intersect.empty();
1219}
1220
1221// Check if InterfacesRemoved payload contains an iface that needs probing.
Patrick Williamsb7077432024-08-16 15:22:21 -04001222static bool irContainsProbeInterface(
1223 sdbusplus::message_t& msg, const std::set<std::string>& probeInterfaces)
Matt Spinler8d1ac3a2023-02-02 09:48:04 -06001224{
1225 sdbusplus::message::object_path path;
1226 std::set<std::string> interfaces;
1227 std::set<std::string> intersect;
1228
1229 msg.read(path, interfaces);
1230
1231 std::set_intersection(interfaces.begin(), interfaces.end(),
1232 probeInterfaces.begin(), probeInterfaces.end(),
1233 std::inserter(intersect, intersect.end()));
1234 return !intersect.empty();
1235}
1236
James Feist98132792019-07-09 13:29:09 -07001237int main()
James Feist75fdeeb2018-02-20 14:26:16 -08001238{
1239 // setup connection to dbus
Ed Tanous07d467b2021-02-23 14:48:37 -08001240 systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1241 systemBus->request_name("xyz.openbmc_project.EntityManager");
James Feist4131aea2018-03-09 09:47:30 -08001242
Nan Zhoua3315672022-09-20 19:48:14 +00001243 // The EntityManager object itself doesn't expose any properties.
1244 // No need to set up ObjectManager for the |EntityManager| object.
1245 sdbusplus::asio::object_server objServer(systemBus, /*skipManager=*/true);
1246
1247 // All other objects that EntityManager currently support are under the
1248 // inventory subtree.
1249 // See the discussion at
1250 // https://discord.com/channels/775381525260664832/1018929092009144380
1251 objServer.add_manager("/xyz/openbmc_project/inventory");
James Feistfd1264a2018-05-03 12:10:00 -07001252
James Feist8f2710a2018-05-09 17:18:55 -07001253 std::shared_ptr<sdbusplus::asio::dbus_interface> entityIface =
1254 objServer.add_interface("/xyz/openbmc_project/EntityManager",
1255 "xyz.openbmc_project.EntityManager");
James Feistfd1264a2018-05-03 12:10:00 -07001256
James Feist4131aea2018-03-09 09:47:30 -08001257 // to keep reference to the match / filter objects so they don't get
1258 // destroyed
James Feist8f2710a2018-05-09 17:18:55 -07001259
1260 nlohmann::json systemConfiguration = nlohmann::json::object();
1261
Matt Spinler8d1ac3a2023-02-02 09:48:04 -06001262 std::set<std::string> probeInterfaces = getProbeInterfaces();
1263
Brad Bishopc76af0f2020-12-04 13:50:23 -05001264 // We need a poke from DBus for static providers that create all their
1265 // objects prior to claiming a well-known name, and thus don't emit any
1266 // org.freedesktop.DBus.Properties signals. Similarly if a process exits
1267 // for any reason, expected or otherwise, we'll need a poke to remove
1268 // entities from DBus.
Patrick Williams2af39222022-07-22 19:26:56 -05001269 sdbusplus::bus::match_t nameOwnerChangedMatch(
1270 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishopc76af0f2020-12-04 13:50:23 -05001271 sdbusplus::bus::match::rules::nameOwnerChanged(),
Patrick Williams7b8786f2022-10-10 10:23:37 -05001272 [&](sdbusplus::message_t& m) {
Patrick Williamsb7077432024-08-16 15:22:21 -04001273 auto [name, oldOwner,
1274 newOwner] = m.unpack<std::string, std::string, std::string>();
Patrick Williams7b8786f2022-10-10 10:23:37 -05001275
Patrick Williamsb7077432024-08-16 15:22:21 -04001276 if (name.starts_with(':'))
1277 {
1278 // We should do nothing with unique-name connections.
1279 return;
1280 }
Patrick Williams7b8786f2022-10-10 10:23:37 -05001281
Patrick Williamsb7077432024-08-16 15:22:21 -04001282 propertiesChangedCallback(systemConfiguration, objServer);
1283 });
Brad Bishop10a8c5f2020-12-07 21:40:07 -05001284 // We also need a poke from DBus when new interfaces are created or
1285 // destroyed.
Patrick Williams2af39222022-07-22 19:26:56 -05001286 sdbusplus::bus::match_t interfacesAddedMatch(
1287 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishop10a8c5f2020-12-07 21:40:07 -05001288 sdbusplus::bus::match::rules::interfacesAdded(),
Matt Spinler8d1ac3a2023-02-02 09:48:04 -06001289 [&](sdbusplus::message_t& msg) {
Patrick Williamsb7077432024-08-16 15:22:21 -04001290 if (iaContainsProbeInterface(msg, probeInterfaces))
1291 {
1292 propertiesChangedCallback(systemConfiguration, objServer);
1293 }
1294 });
Patrick Williams2af39222022-07-22 19:26:56 -05001295 sdbusplus::bus::match_t interfacesRemovedMatch(
1296 static_cast<sdbusplus::bus_t&>(*systemBus),
Brad Bishop10a8c5f2020-12-07 21:40:07 -05001297 sdbusplus::bus::match::rules::interfacesRemoved(),
Matt Spinler8d1ac3a2023-02-02 09:48:04 -06001298 [&](sdbusplus::message_t& msg) {
Patrick Williamsb7077432024-08-16 15:22:21 -04001299 if (irContainsProbeInterface(msg, probeInterfaces))
1300 {
1301 propertiesChangedCallback(systemConfiguration, objServer);
1302 }
1303 });
Brad Bishopc76af0f2020-12-04 13:50:23 -05001304
Ed Tanous49a888c2023-03-06 13:44:51 -08001305 boost::asio::post(io, [&]() {
1306 propertiesChangedCallback(systemConfiguration, objServer);
1307 });
James Feist4131aea2018-03-09 09:47:30 -08001308
James Feistfd1264a2018-05-03 12:10:00 -07001309 entityIface->register_method("ReScan", [&]() {
James Feist4dc617b2020-05-01 09:54:47 -07001310 propertiesChangedCallback(systemConfiguration, objServer);
James Feist75fdeeb2018-02-20 14:26:16 -08001311 });
John Edward Broadbentd97c6312023-10-26 20:32:07 +00001312 tryIfaceInitialize(entityIface);
James Feist8f2710a2018-05-09 17:18:55 -07001313
James Feist1df06a42019-04-11 14:23:04 -07001314 if (fwVersionIsSame())
1315 {
1316 if (std::filesystem::is_regular_file(currentConfiguration))
1317 {
1318 // this file could just be deleted, but it's nice for debug
1319 std::filesystem::create_directory(tempConfigDir);
1320 std::filesystem::remove(lastConfiguration);
1321 std::filesystem::copy(currentConfiguration, lastConfiguration);
1322 std::filesystem::remove(currentConfiguration);
1323
1324 std::ifstream jsonStream(lastConfiguration);
1325 if (jsonStream.good())
1326 {
1327 auto data = nlohmann::json::parse(jsonStream, nullptr, false);
1328 if (data.is_discarded())
1329 {
Patrick Williamsb7077432024-08-16 15:22:21 -04001330 std::cerr
1331 << "syntax error in " << lastConfiguration << "\n";
James Feist1df06a42019-04-11 14:23:04 -07001332 }
1333 else
1334 {
1335 lastJson = std::move(data);
1336 }
1337 }
1338 else
1339 {
1340 std::cerr << "unable to open " << lastConfiguration << "\n";
1341 }
1342 }
1343 }
1344 else
1345 {
1346 // not an error, just logging at this level to make it in the journal
1347 std::cerr << "Clearing previous configuration\n";
1348 std::filesystem::remove(currentConfiguration);
1349 }
1350
1351 // some boards only show up after power is on, we want to not say they are
1352 // removed until the same state happens
Ed Tanous07d467b2021-02-23 14:48:37 -08001353 setupPowerMatch(systemBus);
James Feist1df06a42019-04-11 14:23:04 -07001354
James Feist1b2e2242018-01-30 13:45:19 -08001355 io.run();
James Feist3cb5fec2018-01-23 14:41:51 -08001356
1357 return 0;
James Feist75fdeeb2018-02-20 14:26:16 -08001358}