blob: 04f3cc9e76503095698325d432d2c47270844090 [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 Bishop1fb9f3f2020-08-28 08:15:13 -040016/// \file EntityManager.cpp
James Feist3cb5fec2018-01-23 14:41:51 -080017
James Feist1df06a42019-04-11 14:23:04 -070018#include "EntityManager.hpp"
19
Patrick Venturea49dc332019-10-26 08:32:02 -070020#include "Overlay.hpp"
21#include "Utils.hpp"
James Feist481c5d52019-08-13 14:40:40 -070022#include "VariantVisitors.hpp"
23
James Feist11be6672018-04-06 14:05:32 -070024#include <boost/algorithm/string/case_conv.hpp>
James Feistf5125b02019-06-06 11:27:43 -070025#include <boost/algorithm/string/classification.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080026#include <boost/algorithm/string/predicate.hpp>
27#include <boost/algorithm/string/replace.hpp>
James Feistf5125b02019-06-06 11:27:43 -070028#include <boost/algorithm/string/split.hpp>
James Feist02d2b932020-02-06 16:28:48 -080029#include <boost/asio/io_context.hpp>
James Feistb1728ca2020-04-30 15:40:55 -070030#include <boost/asio/steady_timer.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080031#include <boost/container/flat_map.hpp>
32#include <boost/container/flat_set.hpp>
James Feistf5125b02019-06-06 11:27:43 -070033#include <boost/range/iterator_range.hpp>
James Feist8c505da2020-05-28 10:06:33 -070034#include <nlohmann/json.hpp>
35#include <sdbusplus/asio/connection.hpp>
36#include <sdbusplus/asio/object_server.hpp>
37
Igor Kononenko9fd87e52020-10-06 01:18:17 +030038#include <charconv>
James Feist637b3ef2019-04-15 16:35:30 -070039#include <filesystem>
James Feista465ccc2019-02-08 12:51:01 -080040#include <fstream>
41#include <iostream>
Andrew Jeffery666583b2021-12-01 15:50:12 +103042#include <map>
James Feista465ccc2019-02-08 12:51:01 -080043#include <regex>
James Feista465ccc2019-02-08 12:51:01 -080044#include <variant>
Andrew Jefferya9c58922021-06-01 09:28:59 +093045constexpr const char* hostConfigurationDirectory = SYSCONF_DIR "configurations";
James Feista465ccc2019-02-08 12:51:01 -080046constexpr const char* configurationDirectory = PACKAGE_DIR "configurations";
47constexpr const char* schemaDirectory = PACKAGE_DIR "configurations/schemas";
James Feist1df06a42019-04-11 14:23:04 -070048constexpr const char* tempConfigDir = "/tmp/configuration/";
49constexpr const char* lastConfiguration = "/tmp/configuration/last.json";
50constexpr const char* currentConfiguration = "/var/configuration/system.json";
James Feista465ccc2019-02-08 12:51:01 -080051constexpr const char* globalSchema = "global.json";
James Feistf1b14142019-04-10 15:22:09 -070052
Andrew Jeffery666583b2021-12-01 15:50:12 +103053const boost::container::flat_map<const char*, probe_type_codes, CmpStr>
Ed Tanous07d467b2021-02-23 14:48:37 -080054 probeTypes{{{"FALSE", probe_type_codes::FALSE_T},
55 {"TRUE", probe_type_codes::TRUE_T},
56 {"AND", probe_type_codes::AND},
57 {"OR", probe_type_codes::OR},
58 {"FOUND", probe_type_codes::FOUND},
59 {"MATCH_ONE", probe_type_codes::MATCH_ONE}}};
James Feist3cb5fec2018-01-23 14:41:51 -080060
Adrian Ambrożewiczc789fca2020-05-14 15:50:05 +020061static constexpr std::array<const char*, 6> settableInterfaces = {
62 "FanProfile", "Pid", "Pid.Zone", "Stepwise", "Thresholds", "Polling"};
James Feist68500ff2018-08-08 15:40:42 -070063using JsonVariantType =
James Feist338b8a72019-03-01 10:16:45 -080064 std::variant<std::vector<std::string>, std::vector<double>, std::string,
65 int64_t, uint64_t, double, int32_t, uint32_t, int16_t,
66 uint16_t, uint8_t, bool>;
James Feist3cb5fec2018-01-23 14:41:51 -080067
68using ManagedObjectType = boost::container::flat_map<
James Feist8f2710a2018-05-09 17:18:55 -070069 sdbusplus::message::object_path,
James Feist3cb5fec2018-01-23 14:41:51 -080070 boost::container::flat_map<
71 std::string,
James Feist8f2710a2018-05-09 17:18:55 -070072 boost::container::flat_map<std::string, BasicVariantType>>>;
James Feist3cb5fec2018-01-23 14:41:51 -080073
James Feistd58879a82019-09-11 11:26:07 -070074// store reference to all interfaces so we can destroy them later
75boost::container::flat_map<
James Feist02d2b932020-02-06 16:28:48 -080076 std::string, std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>>
James Feistd58879a82019-09-11 11:26:07 -070077 inventory;
78
James Feist3cb5fec2018-01-23 14:41:51 -080079// todo: pass this through nicer
Ed Tanous07d467b2021-02-23 14:48:37 -080080std::shared_ptr<sdbusplus::asio::connection> systemBus;
Andrew Jeffery47af65a2021-12-01 14:16:31 +103081nlohmann::json lastJson;
James Feist3cb5fec2018-01-23 14:41:51 -080082
James Feist02d2b932020-02-06 16:28:48 -080083boost::asio::io_context io;
84
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
Andrew Jeffery666583b2021-12-01 15:50:12 +103088FoundProbeTypeT findProbeType(const std::string& probe)
89{
90 boost::container::flat_map<const char*, probe_type_codes,
91 CmpStr>::const_iterator probeType;
92 for (probeType = probeTypes.begin(); probeType != probeTypes.end();
93 ++probeType)
94 {
95 if (probe.find(probeType->first) != std::string::npos)
96 {
97 return probeType;
98 }
99 }
100
101 return std::nullopt;
102}
103
James Feistd58879a82019-09-11 11:26:07 -0700104static std::shared_ptr<sdbusplus::asio::dbus_interface>
105 createInterface(sdbusplus::asio::object_server& objServer,
106 const std::string& path, const std::string& interface,
James Feist02d2b932020-02-06 16:28:48 -0800107 const std::string& parent, bool checkNull = false)
James Feistd58879a82019-09-11 11:26:07 -0700108{
James Feist02d2b932020-02-06 16:28:48 -0800109 // on first add we have no reason to check for null before add, as there
110 // won't be any. For dynamically added interfaces, we check for null so that
111 // a constant delete/add will not create a memory leak
112
113 auto ptr = objServer.add_interface(path, interface);
114 auto& dataVector = inventory[parent];
115 if (checkNull)
116 {
117 auto it = std::find_if(dataVector.begin(), dataVector.end(),
118 [](const auto& p) { return p.expired(); });
119 if (it != dataVector.end())
120 {
121 *it = ptr;
122 return ptr;
123 }
124 }
125 dataVector.emplace_back(ptr);
126 return ptr;
James Feistd58879a82019-09-11 11:26:07 -0700127}
128
James Feist8f2710a2018-05-09 17:18:55 -0700129// writes output files to persist data
James Feista465ccc2019-02-08 12:51:01 -0800130bool writeJsonFiles(const nlohmann::json& systemConfiguration)
James Feist1b2e2242018-01-30 13:45:19 -0800131{
James Feist1df06a42019-04-11 14:23:04 -0700132 std::filesystem::create_directory(configurationOutDir);
133 std::ofstream output(currentConfiguration);
James Feistbb43d022018-06-12 15:44:33 -0700134 if (!output.good())
135 {
136 return false;
137 }
James Feist1b2e2242018-01-30 13:45:19 -0800138 output << systemConfiguration.dump(4);
139 output.close();
James Feistbb43d022018-06-12 15:44:33 -0700140 return true;
James Feist8f2710a2018-05-09 17:18:55 -0700141}
James Feist1b2e2242018-01-30 13:45:19 -0800142
James Feist97a63f12018-05-17 13:50:57 -0700143template <typename JsonType>
James Feista465ccc2019-02-08 12:51:01 -0800144bool setJsonFromPointer(const std::string& ptrStr, const JsonType& value,
145 nlohmann::json& systemConfiguration)
James Feist97a63f12018-05-17 13:50:57 -0700146{
147 try
148 {
149 nlohmann::json::json_pointer ptr(ptrStr);
James Feista465ccc2019-02-08 12:51:01 -0800150 nlohmann::json& ref = systemConfiguration[ptr];
James Feist97a63f12018-05-17 13:50:57 -0700151 ref = value;
152 return true;
153 }
James Feist98132792019-07-09 13:29:09 -0700154 catch (const std::out_of_range&)
James Feist97a63f12018-05-17 13:50:57 -0700155 {
156 return false;
157 }
158}
James Feistbb43d022018-06-12 15:44:33 -0700159
James Feistebcc26b2019-03-22 12:30:43 -0700160// template function to add array as dbus property
161template <typename PropertyType>
162void addArrayToDbus(const std::string& name, const nlohmann::json& array,
163 sdbusplus::asio::dbus_interface* iface,
164 sdbusplus::asio::PropertyPermission permission,
165 nlohmann::json& systemConfiguration,
166 const std::string& jsonPointerString)
167{
168 std::vector<PropertyType> values;
169 for (const auto& property : array)
170 {
171 auto ptr = property.get_ptr<const PropertyType*>();
172 if (ptr != nullptr)
173 {
174 values.emplace_back(*ptr);
175 }
176 }
177
178 if (permission == sdbusplus::asio::PropertyPermission::readOnly)
179 {
180 iface->register_property(name, values);
181 }
182 else
183 {
184 iface->register_property(
185 name, values,
186 [&systemConfiguration,
187 jsonPointerString{std::string(jsonPointerString)}](
188 const std::vector<PropertyType>& newVal,
189 std::vector<PropertyType>& val) {
190 val = newVal;
191 if (!setJsonFromPointer(jsonPointerString, val,
192 systemConfiguration))
193 {
194 std::cerr << "error setting json field\n";
195 return -1;
196 }
197 if (!writeJsonFiles(systemConfiguration))
198 {
199 std::cerr << "error setting json file\n";
200 return -1;
201 }
202 return 1;
203 });
204 }
205}
206
James Feistbb43d022018-06-12 15:44:33 -0700207template <typename PropertyType>
James Feista465ccc2019-02-08 12:51:01 -0800208void addProperty(const std::string& propertyName, const PropertyType& value,
209 sdbusplus::asio::dbus_interface* iface,
210 nlohmann::json& systemConfiguration,
211 const std::string& jsonPointerString,
James Feistbb43d022018-06-12 15:44:33 -0700212 sdbusplus::asio::PropertyPermission permission)
213{
214 if (permission == sdbusplus::asio::PropertyPermission::readOnly)
215 {
216 iface->register_property(propertyName, value);
217 return;
218 }
James Feist68500ff2018-08-08 15:40:42 -0700219 iface->register_property(
220 propertyName, value,
221 [&systemConfiguration,
222 jsonPointerString{std::string(jsonPointerString)}](
James Feista465ccc2019-02-08 12:51:01 -0800223 const PropertyType& newVal, PropertyType& val) {
James Feist68500ff2018-08-08 15:40:42 -0700224 val = newVal;
225 if (!setJsonFromPointer(jsonPointerString, val,
226 systemConfiguration))
227 {
228 std::cerr << "error setting json field\n";
229 return -1;
230 }
James Feistc6248a52018-08-14 10:09:45 -0700231 if (!writeJsonFiles(systemConfiguration))
James Feist68500ff2018-08-08 15:40:42 -0700232 {
233 std::cerr << "error setting json file\n";
James Feistc6248a52018-08-14 10:09:45 -0700234 return -1;
235 }
236 return 1;
237 });
238}
239
240void createDeleteObjectMethod(
James Feista465ccc2019-02-08 12:51:01 -0800241 const std::string& jsonPointerPath,
242 const std::shared_ptr<sdbusplus::asio::dbus_interface>& iface,
243 sdbusplus::asio::object_server& objServer,
244 nlohmann::json& systemConfiguration)
James Feistc6248a52018-08-14 10:09:45 -0700245{
246 std::weak_ptr<sdbusplus::asio::dbus_interface> interface = iface;
247 iface->register_method(
248 "Delete", [&objServer, &systemConfiguration, interface,
249 jsonPointerPath{std::string(jsonPointerPath)}]() {
James Feist98132792019-07-09 13:29:09 -0700250 std::shared_ptr<sdbusplus::asio::dbus_interface> dbusInterface =
James Feistc6248a52018-08-14 10:09:45 -0700251 interface.lock();
James Feist98132792019-07-09 13:29:09 -0700252 if (!dbusInterface)
James Feistc6248a52018-08-14 10:09:45 -0700253 {
254 // this technically can't happen as the pointer is pointing to
255 // us
256 throw DBusInternalError();
257 }
258 nlohmann::json::json_pointer ptr(jsonPointerPath);
James Feistc6248a52018-08-14 10:09:45 -0700259 systemConfiguration[ptr] = nullptr;
260
James Feist02d2b932020-02-06 16:28:48 -0800261 // todo(james): dig through sdbusplus to find out why we can't
262 // delete it in a method call
263 io.post([&objServer, dbusInterface]() mutable {
264 objServer.remove_interface(dbusInterface);
265 });
266
James Feistc6248a52018-08-14 10:09:45 -0700267 if (!writeJsonFiles(systemConfiguration))
268 {
269 std::cerr << "error setting json file\n";
270 throw DBusInternalError();
James Feist68500ff2018-08-08 15:40:42 -0700271 }
James Feist68500ff2018-08-08 15:40:42 -0700272 });
James Feistbb43d022018-06-12 15:44:33 -0700273}
274
James Feist1b2e2242018-01-30 13:45:19 -0800275// adds simple json types to interface's properties
James Feistbb43d022018-06-12 15:44:33 -0700276void populateInterfaceFromJson(
James Feista465ccc2019-02-08 12:51:01 -0800277 nlohmann::json& systemConfiguration, const std::string& jsonPointerPath,
278 std::shared_ptr<sdbusplus::asio::dbus_interface>& iface,
279 nlohmann::json& dict, sdbusplus::asio::object_server& objServer,
James Feistbb43d022018-06-12 15:44:33 -0700280 sdbusplus::asio::PropertyPermission permission =
281 sdbusplus::asio::PropertyPermission::readOnly)
James Feist1b2e2242018-01-30 13:45:19 -0800282{
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030283 for (auto& [key, value] : dict.items())
James Feist1b2e2242018-01-30 13:45:19 -0800284 {
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030285 auto type = value.type();
James Feist8f2710a2018-05-09 17:18:55 -0700286 bool array = false;
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030287 if (value.type() == nlohmann::json::value_t::array)
James Feist8f2710a2018-05-09 17:18:55 -0700288 {
289 array = true;
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030290 if (!value.size())
James Feist8f2710a2018-05-09 17:18:55 -0700291 {
292 continue;
293 }
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030294 type = value[0].type();
James Feist8f2710a2018-05-09 17:18:55 -0700295 bool isLegal = true;
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030296 for (const auto& arrayItem : value)
James Feist8f2710a2018-05-09 17:18:55 -0700297 {
298 if (arrayItem.type() != type)
299 {
300 isLegal = false;
301 break;
302 }
303 }
304 if (!isLegal)
305 {
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030306 std::cerr << "dbus format error" << value << "\n";
James Feist8f2710a2018-05-09 17:18:55 -0700307 continue;
308 }
James Feista218ddb2019-04-11 14:01:31 -0700309 }
310 if (type == nlohmann::json::value_t::object)
311 {
312 continue; // handled elsewhere
James Feist8f2710a2018-05-09 17:18:55 -0700313 }
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030314
315 std::string path = jsonPointerPath;
316 path.append("/").append(key);
James Feistbb43d022018-06-12 15:44:33 -0700317 if (permission == sdbusplus::asio::PropertyPermission::readWrite)
318 {
319 // all setable numbers are doubles as it is difficult to always
320 // create a configuration file with all whole numbers as decimals
321 // i.e. 1.0
James Feistebcc26b2019-03-22 12:30:43 -0700322 if (array)
323 {
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030324 if (value[0].is_number())
James Feistebcc26b2019-03-22 12:30:43 -0700325 {
326 type = nlohmann::json::value_t::number_float;
327 }
328 }
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030329 else if (value.is_number())
James Feistbb43d022018-06-12 15:44:33 -0700330 {
331 type = nlohmann::json::value_t::number_float;
332 }
333 }
334
James Feist8f2710a2018-05-09 17:18:55 -0700335 switch (type)
James Feist1b2e2242018-01-30 13:45:19 -0800336 {
James Feist9eb0b582018-04-27 12:15:46 -0700337 case (nlohmann::json::value_t::boolean):
338 {
James Feist8f2710a2018-05-09 17:18:55 -0700339 if (array)
340 {
341 // todo: array of bool isn't detected correctly by
342 // sdbusplus, change it to numbers
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030343 addArrayToDbus<uint64_t>(key, value, iface.get(),
344 permission, systemConfiguration,
345 path);
James Feist8f2710a2018-05-09 17:18:55 -0700346 }
James Feistbb43d022018-06-12 15:44:33 -0700347
James Feist97a63f12018-05-17 13:50:57 -0700348 else
349 {
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030350 addProperty(key, value.get<bool>(), iface.get(),
351 systemConfiguration, path, permission);
James Feist97a63f12018-05-17 13:50:57 -0700352 }
James Feist9eb0b582018-04-27 12:15:46 -0700353 break;
354 }
355 case (nlohmann::json::value_t::number_integer):
356 {
James Feist8f2710a2018-05-09 17:18:55 -0700357 if (array)
358 {
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030359 addArrayToDbus<int64_t>(key, value, iface.get(), permission,
Andrew Jeffery029ee282022-03-25 13:11:36 +1030360 systemConfiguration, path);
James Feist97a63f12018-05-17 13:50:57 -0700361 }
362 else
363 {
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030364 addProperty(key, value.get<int64_t>(), iface.get(),
365 systemConfiguration, path,
James Feistbb43d022018-06-12 15:44:33 -0700366 sdbusplus::asio::PropertyPermission::readOnly);
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_unsigned):
371 {
James Feist8f2710a2018-05-09 17:18:55 -0700372 if (array)
373 {
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030374 addArrayToDbus<uint64_t>(key, value, iface.get(),
375 permission, systemConfiguration,
376 path);
James Feist97a63f12018-05-17 13:50:57 -0700377 }
378 else
379 {
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030380 addProperty(key, value.get<uint64_t>(), iface.get(),
Andrew Jeffery029ee282022-03-25 13:11:36 +1030381 systemConfiguration, path,
James Feistbb43d022018-06-12 15:44:33 -0700382 sdbusplus::asio::PropertyPermission::readOnly);
James Feist97a63f12018-05-17 13:50:57 -0700383 }
James Feist9eb0b582018-04-27 12:15:46 -0700384 break;
385 }
386 case (nlohmann::json::value_t::number_float):
387 {
James Feist8f2710a2018-05-09 17:18:55 -0700388 if (array)
389 {
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030390 addArrayToDbus<double>(key, value, iface.get(), permission,
Andrew Jeffery029ee282022-03-25 13:11:36 +1030391 systemConfiguration, path);
James Feist8f2710a2018-05-09 17:18:55 -0700392 }
James Feistbb43d022018-06-12 15:44:33 -0700393
James Feist97a63f12018-05-17 13:50:57 -0700394 else
395 {
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030396 addProperty(key, value.get<double>(), iface.get(),
397 systemConfiguration, path, permission);
James Feist97a63f12018-05-17 13:50:57 -0700398 }
James Feist9eb0b582018-04-27 12:15:46 -0700399 break;
400 }
401 case (nlohmann::json::value_t::string):
402 {
James Feist8f2710a2018-05-09 17:18:55 -0700403 if (array)
404 {
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030405 addArrayToDbus<std::string>(key, value, iface.get(),
406 permission, systemConfiguration,
407 path);
James Feist97a63f12018-05-17 13:50:57 -0700408 }
409 else
410 {
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030411 addProperty(key, value.get<std::string>(), iface.get(),
412 systemConfiguration, path, permission);
James Feist97a63f12018-05-17 13:50:57 -0700413 }
James Feist9eb0b582018-04-27 12:15:46 -0700414 break;
415 }
James Feist0eb40352019-04-09 14:44:04 -0700416 default:
417 {
James Feista218ddb2019-04-11 14:01:31 -0700418 std::cerr << "Unexpected json type in system configuration "
Andrew Jeffery65ea4502022-03-25 13:15:34 +1030419 << key << ": " << value.type_name() << "\n";
James Feist0eb40352019-04-09 14:44:04 -0700420 break;
421 }
James Feist1b2e2242018-01-30 13:45:19 -0800422 }
423 }
James Feistc6248a52018-08-14 10:09:45 -0700424 if (permission == sdbusplus::asio::PropertyPermission::readWrite)
425 {
426 createDeleteObjectMethod(jsonPointerPath, iface, objServer,
427 systemConfiguration);
428 }
James Feist8f2710a2018-05-09 17:18:55 -0700429 iface->initialize();
James Feist1b2e2242018-01-30 13:45:19 -0800430}
431
James Feista465ccc2019-02-08 12:51:01 -0800432sdbusplus::asio::PropertyPermission getPermission(const std::string& interface)
James Feistc6248a52018-08-14 10:09:45 -0700433{
434 return std::find(settableInterfaces.begin(), settableInterfaces.end(),
435 interface) != settableInterfaces.end()
436 ? sdbusplus::asio::PropertyPermission::readWrite
437 : sdbusplus::asio::PropertyPermission::readOnly;
438}
439
James Feista465ccc2019-02-08 12:51:01 -0800440void createAddObjectMethod(const std::string& jsonPointerPath,
441 const std::string& path,
442 nlohmann::json& systemConfiguration,
James Feistd58879a82019-09-11 11:26:07 -0700443 sdbusplus::asio::object_server& objServer,
444 const std::string& board)
James Feist68500ff2018-08-08 15:40:42 -0700445{
James Feistd58879a82019-09-11 11:26:07 -0700446 std::shared_ptr<sdbusplus::asio::dbus_interface> iface = createInterface(
447 objServer, path, "xyz.openbmc_project.AddObject", board);
James Feist68500ff2018-08-08 15:40:42 -0700448
449 iface->register_method(
450 "AddObject",
451 [&systemConfiguration, &objServer,
James Feistd58879a82019-09-11 11:26:07 -0700452 jsonPointerPath{std::string(jsonPointerPath)}, path{std::string(path)},
453 board](const boost::container::flat_map<std::string, JsonVariantType>&
454 data) {
James Feist68500ff2018-08-08 15:40:42 -0700455 nlohmann::json::json_pointer ptr(jsonPointerPath);
James Feista465ccc2019-02-08 12:51:01 -0800456 nlohmann::json& base = systemConfiguration[ptr];
James Feist68500ff2018-08-08 15:40:42 -0700457 auto findExposes = base.find("Exposes");
458
459 if (findExposes == base.end())
460 {
461 throw std::invalid_argument("Entity must have children.");
462 }
463
464 // this will throw invalid-argument to sdbusplus if invalid json
465 nlohmann::json newData{};
James Feista465ccc2019-02-08 12:51:01 -0800466 for (const auto& item : data)
James Feist68500ff2018-08-08 15:40:42 -0700467 {
James Feista465ccc2019-02-08 12:51:01 -0800468 nlohmann::json& newJson = newData[item.first];
469 std::visit([&newJson](auto&& val) { newJson = std::move(val); },
470 item.second);
James Feist68500ff2018-08-08 15:40:42 -0700471 }
472
473 auto findName = newData.find("Name");
474 auto findType = newData.find("Type");
475 if (findName == newData.end() || findType == newData.end())
476 {
477 throw std::invalid_argument("AddObject missing Name or Type");
478 }
James Feista465ccc2019-02-08 12:51:01 -0800479 const std::string* type = findType->get_ptr<const std::string*>();
480 const std::string* name = findName->get_ptr<const std::string*>();
James Feist68500ff2018-08-08 15:40:42 -0700481 if (type == nullptr || name == nullptr)
482 {
483 throw std::invalid_argument("Type and Name must be a string.");
484 }
485
James Feist02d2b932020-02-06 16:28:48 -0800486 bool foundNull = false;
James Feist68500ff2018-08-08 15:40:42 -0700487 size_t lastIndex = 0;
488 // we add in the "exposes"
James Feist02d2b932020-02-06 16:28:48 -0800489 for (const auto& expose : *findExposes)
James Feist68500ff2018-08-08 15:40:42 -0700490 {
James Feist02d2b932020-02-06 16:28:48 -0800491 if (expose.is_null())
492 {
493 foundNull = true;
494 continue;
495 }
496
497 if (expose["Name"] == *name && expose["Type"] == *type)
James Feist68500ff2018-08-08 15:40:42 -0700498 {
499 throw std::invalid_argument(
500 "Field already in JSON, not adding");
501 }
James Feist02d2b932020-02-06 16:28:48 -0800502
503 if (foundNull)
504 {
505 continue;
506 }
507
James Feist68500ff2018-08-08 15:40:42 -0700508 lastIndex++;
509 }
510
511 std::ifstream schemaFile(std::string(schemaDirectory) + "/" +
512 *type + ".json");
513 // todo(james) we might want to also make a list of 'can add'
514 // interfaces but for now I think the assumption if there is a
515 // schema avaliable that it is allowed to update is fine
516 if (!schemaFile.good())
517 {
518 throw std::invalid_argument(
519 "No schema avaliable, cannot validate.");
520 }
521 nlohmann::json schema =
522 nlohmann::json::parse(schemaFile, nullptr, false);
523 if (schema.is_discarded())
524 {
525 std::cerr << "Schema not legal" << *type << ".json\n";
526 throw DBusInternalError();
527 }
528 if (!validateJson(schema, newData))
529 {
530 throw std::invalid_argument("Data does not match schema");
531 }
James Feist02d2b932020-02-06 16:28:48 -0800532 if (foundNull)
533 {
534 findExposes->at(lastIndex) = newData;
535 }
536 else
537 {
538 findExposes->push_back(newData);
539 }
James Feist68500ff2018-08-08 15:40:42 -0700540 if (!writeJsonFiles(systemConfiguration))
541 {
542 std::cerr << "Error writing json files\n";
543 throw DBusInternalError();
544 }
545 std::string dbusName = *name;
546
547 std::regex_replace(dbusName.begin(), dbusName.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800548 dbusName.end(), illegalDbusMemberRegex, "_");
James Feistd58879a82019-09-11 11:26:07 -0700549
550 std::shared_ptr<sdbusplus::asio::dbus_interface> interface =
551 createInterface(objServer, path + "/" + dbusName,
552 "xyz.openbmc_project.Configuration." + *type,
James Feist02d2b932020-02-06 16:28:48 -0800553 board, true);
James Feist68500ff2018-08-08 15:40:42 -0700554 // permission is read-write, as since we just created it, must be
555 // runtime modifiable
556 populateInterfaceFromJson(
557 systemConfiguration,
James Feist16a02f22019-05-13 15:21:37 -0700558 jsonPointerPath + "/Exposes/" + std::to_string(lastIndex),
James Feist98132792019-07-09 13:29:09 -0700559 interface, newData, objServer,
James Feist68500ff2018-08-08 15:40:42 -0700560 sdbusplus::asio::PropertyPermission::readWrite);
James Feist68500ff2018-08-08 15:40:42 -0700561 });
562 iface->initialize();
563}
564
James Feista465ccc2019-02-08 12:51:01 -0800565void postToDbus(const nlohmann::json& newConfiguration,
566 nlohmann::json& systemConfiguration,
567 sdbusplus::asio::object_server& objServer)
James Feist75fdeeb2018-02-20 14:26:16 -0800568
James Feist1b2e2242018-01-30 13:45:19 -0800569{
James Feist97a63f12018-05-17 13:50:57 -0700570 // iterate through boards
James Feista465ccc2019-02-08 12:51:01 -0800571 for (auto& boardPair : newConfiguration.items())
James Feist1b2e2242018-01-30 13:45:19 -0800572 {
James Feistf1b14142019-04-10 15:22:09 -0700573 std::string boardKey = boardPair.value()["Name"];
James Feistd58879a82019-09-11 11:26:07 -0700574 std::string boardKeyOrig = boardPair.value()["Name"];
James Feistf1b14142019-04-10 15:22:09 -0700575 std::string jsonPointerPath = "/" + boardPair.key();
James Feist97a63f12018-05-17 13:50:57 -0700576 // loop through newConfiguration, but use values from system
577 // configuration to be able to modify via dbus later
James Feistf1b14142019-04-10 15:22:09 -0700578 auto boardValues = systemConfiguration[boardPair.key()];
James Feistd63d18a2018-07-19 15:23:45 -0700579 auto findBoardType = boardValues.find("Type");
James Feist1b2e2242018-01-30 13:45:19 -0800580 std::string boardType;
581 if (findBoardType != boardValues.end() &&
582 findBoardType->type() == nlohmann::json::value_t::string)
583 {
584 boardType = findBoardType->get<std::string>();
585 std::regex_replace(boardType.begin(), boardType.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800586 boardType.end(), illegalDbusMemberRegex, "_");
James Feist1b2e2242018-01-30 13:45:19 -0800587 }
588 else
589 {
590 std::cerr << "Unable to find type for " << boardKey
591 << " reverting to Chassis.\n";
592 boardType = "Chassis";
593 }
James Feist11be6672018-04-06 14:05:32 -0700594 std::string boardtypeLower = boost::algorithm::to_lower_copy(boardType);
James Feist1b2e2242018-01-30 13:45:19 -0800595
596 std::regex_replace(boardKey.begin(), boardKey.begin(), boardKey.end(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800597 illegalDbusMemberRegex, "_");
598 std::string boardName = "/xyz/openbmc_project/inventory/system/";
599 boardName += boardtypeLower;
600 boardName += "/";
601 boardName += boardKey;
James Feist1b2e2242018-01-30 13:45:19 -0800602
James Feistd58879a82019-09-11 11:26:07 -0700603 std::shared_ptr<sdbusplus::asio::dbus_interface> inventoryIface =
604 createInterface(objServer, boardName,
605 "xyz.openbmc_project.Inventory.Item", boardKey);
James Feist68500ff2018-08-08 15:40:42 -0700606
James Feistd58879a82019-09-11 11:26:07 -0700607 std::shared_ptr<sdbusplus::asio::dbus_interface> boardIface =
608 createInterface(objServer, boardName,
609 "xyz.openbmc_project.Inventory.Item." + boardType,
610 boardKeyOrig);
James Feist11be6672018-04-06 14:05:32 -0700611
James Feist68500ff2018-08-08 15:40:42 -0700612 createAddObjectMethod(jsonPointerPath, boardName, systemConfiguration,
James Feistd58879a82019-09-11 11:26:07 -0700613 objServer, boardKeyOrig);
James Feist68500ff2018-08-08 15:40:42 -0700614
James Feist97a63f12018-05-17 13:50:57 -0700615 populateInterfaceFromJson(systemConfiguration, jsonPointerPath,
James Feistc6248a52018-08-14 10:09:45 -0700616 boardIface, boardValues, objServer);
James Feist97a63f12018-05-17 13:50:57 -0700617 jsonPointerPath += "/";
618 // iterate through board properties
James Feista465ccc2019-02-08 12:51:01 -0800619 for (auto& boardField : boardValues.items())
James Feist11be6672018-04-06 14:05:32 -0700620 {
621 if (boardField.value().type() == nlohmann::json::value_t::object)
622 {
James Feistd58879a82019-09-11 11:26:07 -0700623 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
624 createInterface(objServer, boardName, boardField.key(),
625 boardKeyOrig);
626
James Feistc6248a52018-08-14 10:09:45 -0700627 populateInterfaceFromJson(systemConfiguration,
628 jsonPointerPath + boardField.key(),
629 iface, boardField.value(), objServer);
James Feist11be6672018-04-06 14:05:32 -0700630 }
631 }
James Feist97a63f12018-05-17 13:50:57 -0700632
James Feist1e3e6982018-08-03 16:09:28 -0700633 auto exposes = boardValues.find("Exposes");
James Feist1b2e2242018-01-30 13:45:19 -0800634 if (exposes == boardValues.end())
635 {
636 continue;
637 }
James Feist97a63f12018-05-17 13:50:57 -0700638 // iterate through exposes
James Feist1e3e6982018-08-03 16:09:28 -0700639 jsonPointerPath += "Exposes/";
James Feist97a63f12018-05-17 13:50:57 -0700640
641 // store the board level pointer so we can modify it on the way down
642 std::string jsonPointerPathBoard = jsonPointerPath;
643 size_t exposesIndex = -1;
James Feista465ccc2019-02-08 12:51:01 -0800644 for (auto& item : *exposes)
James Feist1b2e2242018-01-30 13:45:19 -0800645 {
James Feist97a63f12018-05-17 13:50:57 -0700646 exposesIndex++;
647 jsonPointerPath = jsonPointerPathBoard;
648 jsonPointerPath += std::to_string(exposesIndex);
649
James Feistd63d18a2018-07-19 15:23:45 -0700650 auto findName = item.find("Name");
James Feist1b2e2242018-01-30 13:45:19 -0800651 if (findName == item.end())
652 {
653 std::cerr << "cannot find name in field " << item << "\n";
654 continue;
655 }
James Feist1e3e6982018-08-03 16:09:28 -0700656 auto findStatus = item.find("Status");
James Feist1b2e2242018-01-30 13:45:19 -0800657 // if status is not found it is assumed to be status = 'okay'
658 if (findStatus != item.end())
659 {
660 if (*findStatus == "disabled")
661 {
662 continue;
663 }
664 }
James Feistd63d18a2018-07-19 15:23:45 -0700665 auto findType = item.find("Type");
James Feist1b2e2242018-01-30 13:45:19 -0800666 std::string itemType;
667 if (findType != item.end())
668 {
669 itemType = findType->get<std::string>();
670 std::regex_replace(itemType.begin(), itemType.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800671 itemType.end(), illegalDbusPathRegex, "_");
James Feist1b2e2242018-01-30 13:45:19 -0800672 }
673 else
674 {
675 itemType = "unknown";
676 }
677 std::string itemName = findName->get<std::string>();
678 std::regex_replace(itemName.begin(), itemName.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800679 itemName.end(), illegalDbusMemberRegex, "_");
680 std::string ifacePath = boardName;
681 ifacePath += "/";
682 ifacePath += itemName;
James Feistc6248a52018-08-14 10:09:45 -0700683
James Feistd58879a82019-09-11 11:26:07 -0700684 std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface =
Ed Tanous07d467b2021-02-23 14:48:37 -0800685 createInterface(objServer, ifacePath,
James Feistd58879a82019-09-11 11:26:07 -0700686 "xyz.openbmc_project.Configuration." + itemType,
687 boardKeyOrig);
James Feist1b2e2242018-01-30 13:45:19 -0800688
James Feist97a63f12018-05-17 13:50:57 -0700689 populateInterfaceFromJson(systemConfiguration, jsonPointerPath,
James Feistc6248a52018-08-14 10:09:45 -0700690 itemIface, item, objServer,
691 getPermission(itemType));
James Feist1b2e2242018-01-30 13:45:19 -0800692
James Feista465ccc2019-02-08 12:51:01 -0800693 for (auto& objectPair : item.items())
James Feist1b2e2242018-01-30 13:45:19 -0800694 {
James Feist97a63f12018-05-17 13:50:57 -0700695 jsonPointerPath = jsonPointerPathBoard +
696 std::to_string(exposesIndex) + "/" +
697 objectPair.key();
James Feist1b2e2242018-01-30 13:45:19 -0800698 if (objectPair.value().type() ==
699 nlohmann::json::value_t::object)
700 {
James Feistd58879a82019-09-11 11:26:07 -0700701 std::shared_ptr<sdbusplus::asio::dbus_interface>
702 objectIface = createInterface(
Ed Tanous07d467b2021-02-23 14:48:37 -0800703 objServer, ifacePath,
James Feistd58879a82019-09-11 11:26:07 -0700704 "xyz.openbmc_project.Configuration." + itemType +
705 "." + objectPair.key(),
706 boardKeyOrig);
James Feist97a63f12018-05-17 13:50:57 -0700707
Adrian Ambrożewiczc789fca2020-05-14 15:50:05 +0200708 populateInterfaceFromJson(systemConfiguration,
709 jsonPointerPath, objectIface,
710 objectPair.value(), objServer,
711 getPermission(objectPair.key()));
James Feist1b2e2242018-01-30 13:45:19 -0800712 }
713 else if (objectPair.value().type() ==
714 nlohmann::json::value_t::array)
715 {
716 size_t index = 0;
James Feist8f2710a2018-05-09 17:18:55 -0700717 if (!objectPair.value().size())
James Feist1b2e2242018-01-30 13:45:19 -0800718 {
James Feist8f2710a2018-05-09 17:18:55 -0700719 continue;
720 }
721 bool isLegal = true;
722 auto type = objectPair.value()[0].type();
723 if (type != nlohmann::json::value_t::object)
724 {
725 continue;
726 }
727
728 // verify legal json
James Feista465ccc2019-02-08 12:51:01 -0800729 for (const auto& arrayItem : objectPair.value())
James Feist8f2710a2018-05-09 17:18:55 -0700730 {
731 if (arrayItem.type() != type)
James Feist1b2e2242018-01-30 13:45:19 -0800732 {
James Feist8f2710a2018-05-09 17:18:55 -0700733 isLegal = false;
James Feist1b2e2242018-01-30 13:45:19 -0800734 break;
735 }
James Feist8f2710a2018-05-09 17:18:55 -0700736 }
737 if (!isLegal)
738 {
739 std::cerr << "dbus format error" << objectPair.value()
740 << "\n";
741 break;
742 }
743
James Feista465ccc2019-02-08 12:51:01 -0800744 for (auto& arrayItem : objectPair.value())
James Feist8f2710a2018-05-09 17:18:55 -0700745 {
James Feist97a63f12018-05-17 13:50:57 -0700746
James Feistd58879a82019-09-11 11:26:07 -0700747 std::shared_ptr<sdbusplus::asio::dbus_interface>
748 objectIface = createInterface(
Ed Tanous07d467b2021-02-23 14:48:37 -0800749 objServer, ifacePath,
James Feistd58879a82019-09-11 11:26:07 -0700750 "xyz.openbmc_project.Configuration." +
751 itemType + "." + objectPair.key() +
752 std::to_string(index),
753 boardKeyOrig);
754
James Feistc6248a52018-08-14 10:09:45 -0700755 populateInterfaceFromJson(
756 systemConfiguration,
757 jsonPointerPath + "/" + std::to_string(index),
758 objectIface, arrayItem, objServer,
759 getPermission(objectPair.key()));
James Feistbb43d022018-06-12 15:44:33 -0700760 index++;
James Feist1b2e2242018-01-30 13:45:19 -0800761 }
762 }
763 }
764 }
765 }
766}
767
James Feist8f2710a2018-05-09 17:18:55 -0700768// reads json files out of the filesystem
James Feista465ccc2019-02-08 12:51:01 -0800769bool findJsonFiles(std::list<nlohmann::json>& configurations)
James Feist3cb5fec2018-01-23 14:41:51 -0800770{
771 // find configuration files
Ed Tanous072e25d2018-12-16 21:45:20 -0800772 std::vector<std::filesystem::path> jsonPaths;
Andrew Jefferya9c58922021-06-01 09:28:59 +0930773 if (!findFiles(
774 std::vector<std::filesystem::path>{configurationDirectory,
775 hostConfigurationDirectory},
776 R"(.*\.json)", jsonPaths))
James Feist3cb5fec2018-01-23 14:41:51 -0800777 {
778 std::cerr << "Unable to find any configuration files in "
James Feistb4383f42018-08-06 16:54:10 -0700779 << configurationDirectory << "\n";
James Feist75fdeeb2018-02-20 14:26:16 -0800780 return false;
James Feist3cb5fec2018-01-23 14:41:51 -0800781 }
James Feistb4383f42018-08-06 16:54:10 -0700782
783 std::ifstream schemaStream(std::string(schemaDirectory) + "/" +
784 globalSchema);
785 if (!schemaStream.good())
786 {
787 std::cerr
788 << "Cannot open schema file, cannot validate JSON, exiting\n\n";
789 std::exit(EXIT_FAILURE);
Ed Tanous072e25d2018-12-16 21:45:20 -0800790 return false;
James Feistb4383f42018-08-06 16:54:10 -0700791 }
792 nlohmann::json schema = nlohmann::json::parse(schemaStream, nullptr, false);
793 if (schema.is_discarded())
794 {
795 std::cerr
796 << "Illegal schema file detected, cannot validate JSON, exiting\n";
797 std::exit(EXIT_FAILURE);
Ed Tanous072e25d2018-12-16 21:45:20 -0800798 return false;
James Feistb4383f42018-08-06 16:54:10 -0700799 }
800
James Feista465ccc2019-02-08 12:51:01 -0800801 for (auto& jsonPath : jsonPaths)
James Feist3cb5fec2018-01-23 14:41:51 -0800802 {
803 std::ifstream jsonStream(jsonPath.c_str());
804 if (!jsonStream.good())
805 {
806 std::cerr << "unable to open " << jsonPath.string() << "\n";
807 continue;
808 }
809 auto data = nlohmann::json::parse(jsonStream, nullptr, false);
810 if (data.is_discarded())
811 {
812 std::cerr << "syntax error in " << jsonPath.string() << "\n";
813 continue;
814 }
James Feist8da99192019-01-24 08:20:16 -0800815 /*
816 * todo(james): reenable this once less things are in flight
817 *
James Feistb4383f42018-08-06 16:54:10 -0700818 if (!validateJson(schema, data))
819 {
820 std::cerr << "Error validating " << jsonPath.string() << "\n";
821 continue;
822 }
James Feist8da99192019-01-24 08:20:16 -0800823 */
James Feistb4383f42018-08-06 16:54:10 -0700824
James Feist3cb5fec2018-01-23 14:41:51 -0800825 if (data.type() == nlohmann::json::value_t::array)
826 {
James Feista465ccc2019-02-08 12:51:01 -0800827 for (auto& d : data)
James Feist3cb5fec2018-01-23 14:41:51 -0800828 {
829 configurations.emplace_back(d);
830 }
831 }
832 else
833 {
834 configurations.emplace_back(data);
835 }
836 }
Ed Tanous072e25d2018-12-16 21:45:20 -0800837 return true;
James Feist75fdeeb2018-02-20 14:26:16 -0800838}
James Feist3cb5fec2018-01-23 14:41:51 -0800839
James Feistb1728ca2020-04-30 15:40:55 -0700840void startRemovedTimer(boost::asio::steady_timer& timer,
James Feist1df06a42019-04-11 14:23:04 -0700841 nlohmann::json& systemConfiguration)
842{
843 static bool scannedPowerOff = false;
844 static bool scannedPowerOn = false;
845
James Feistfb00f392019-06-25 14:16:48 -0700846 if (systemConfiguration.empty() || lastJson.empty())
847 {
848 return; // not ready yet
849 }
James Feist1df06a42019-04-11 14:23:04 -0700850 if (scannedPowerOn)
851 {
852 return;
853 }
854
855 if (!isPowerOn() && scannedPowerOff)
856 {
857 return;
858 }
859
James Feistb1728ca2020-04-30 15:40:55 -0700860 timer.expires_after(std::chrono::seconds(10));
James Feist1a996582019-05-14 15:10:06 -0700861 timer.async_wait(
862 [&systemConfiguration](const boost::system::error_code& ec) {
863 if (ec == boost::asio::error::operation_aborted)
James Feist1df06a42019-04-11 14:23:04 -0700864 {
James Feist1a996582019-05-14 15:10:06 -0700865 // we were cancelled
866 return;
867 }
868
869 bool powerOff = !isPowerOn();
870 for (const auto& item : lastJson.items())
871 {
872 if (systemConfiguration.find(item.key()) ==
873 systemConfiguration.end())
James Feist1df06a42019-04-11 14:23:04 -0700874 {
James Feist1a996582019-05-14 15:10:06 -0700875 bool isDetectedPowerOn = false;
876 auto powerState = item.value().find("PowerState");
877 if (powerState != item.value().end())
James Feist1df06a42019-04-11 14:23:04 -0700878 {
James Feist1a996582019-05-14 15:10:06 -0700879 auto ptr = powerState->get_ptr<const std::string*>();
880 if (ptr)
James Feist1df06a42019-04-11 14:23:04 -0700881 {
James Feist1a996582019-05-14 15:10:06 -0700882 if (*ptr == "On" || *ptr == "BiosPost")
883 {
884 isDetectedPowerOn = true;
885 }
James Feist1df06a42019-04-11 14:23:04 -0700886 }
887 }
James Feist1a996582019-05-14 15:10:06 -0700888 if (powerOff && isDetectedPowerOn)
889 {
890 // power not on yet, don't know if it's there or not
891 continue;
892 }
893 if (!powerOff && scannedPowerOff && isDetectedPowerOn)
894 {
895 // already logged it when power was off
896 continue;
897 }
James Feist1df06a42019-04-11 14:23:04 -0700898
James Feist1a996582019-05-14 15:10:06 -0700899 logDeviceRemoved(item.value());
900 }
James Feist1df06a42019-04-11 14:23:04 -0700901 }
James Feist1a996582019-05-14 15:10:06 -0700902 scannedPowerOff = true;
903 if (!powerOff)
904 {
905 scannedPowerOn = true;
906 }
907 });
James Feist1df06a42019-04-11 14:23:04 -0700908}
909
James Feist8f2710a2018-05-09 17:18:55 -0700910// main properties changed entry
James Feist4dc617b2020-05-01 09:54:47 -0700911void propertiesChangedCallback(nlohmann::json& systemConfiguration,
912 sdbusplus::asio::object_server& objServer)
James Feist8f2710a2018-05-09 17:18:55 -0700913{
James Feist2539ccd2020-05-01 16:15:08 -0700914 static bool inProgress = false;
James Feistb1728ca2020-04-30 15:40:55 -0700915 static boost::asio::steady_timer timer(io);
James Feist899e17f2019-09-13 11:46:29 -0700916 static size_t instance = 0;
917 instance++;
918 size_t count = instance;
James Feist1df06a42019-04-11 14:23:04 -0700919
James Feistb1728ca2020-04-30 15:40:55 -0700920 timer.expires_after(std::chrono::seconds(5));
James Feist8f2710a2018-05-09 17:18:55 -0700921
922 // setup an async wait as we normally get flooded with new requests
James Feist4dc617b2020-05-01 09:54:47 -0700923 timer.async_wait([&systemConfiguration, &objServer,
James Feist899e17f2019-09-13 11:46:29 -0700924 count](const boost::system::error_code& ec) {
James Feist8f2710a2018-05-09 17:18:55 -0700925 if (ec == boost::asio::error::operation_aborted)
926 {
927 // we were cancelled
928 return;
929 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800930 if (ec)
James Feist8f2710a2018-05-09 17:18:55 -0700931 {
932 std::cerr << "async wait error " << ec << "\n";
933 return;
934 }
935
James Feist2539ccd2020-05-01 16:15:08 -0700936 if (inProgress)
937 {
938 propertiesChangedCallback(systemConfiguration, objServer);
939 return;
940 }
941 inProgress = true;
942
James Feist8f2710a2018-05-09 17:18:55 -0700943 nlohmann::json oldConfiguration = systemConfiguration;
James Feist899e17f2019-09-13 11:46:29 -0700944 auto missingConfigurations = std::make_shared<nlohmann::json>();
945 *missingConfigurations = systemConfiguration;
946
James Feist8f2710a2018-05-09 17:18:55 -0700947 std::list<nlohmann::json> configurations;
948 if (!findJsonFiles(configurations))
949 {
950 std::cerr << "cannot find json files\n";
James Feist2539ccd2020-05-01 16:15:08 -0700951 inProgress = false;
James Feist8f2710a2018-05-09 17:18:55 -0700952 return;
953 }
954
955 auto perfScan = std::make_shared<PerformScan>(
James Feist899e17f2019-09-13 11:46:29 -0700956 systemConfiguration, *missingConfigurations, configurations,
James Feist4dc617b2020-05-01 09:54:47 -0700957 objServer,
958 [&systemConfiguration, &objServer, count, oldConfiguration,
959 missingConfigurations]() {
James Feist899e17f2019-09-13 11:46:29 -0700960 // this is something that since ac has been applied to the bmc
961 // we saw, and we no longer see it
962 bool powerOff = !isPowerOn();
963 for (const auto& item : missingConfigurations->items())
964 {
965 bool isDetectedPowerOn = false;
966 auto powerState = item.value().find("PowerState");
967 if (powerState != item.value().end())
968 {
969 auto ptr = powerState->get_ptr<const std::string*>();
970 if (ptr)
971 {
972 if (*ptr == "On" || *ptr == "BiosPost")
973 {
974 isDetectedPowerOn = true;
975 }
976 }
977 }
978 if (powerOff && isDetectedPowerOn)
979 {
980 // power not on yet, don't know if it's there or not
981 continue;
982 }
983 std::string name = item.value()["Name"].get<std::string>();
James Feist02d2b932020-02-06 16:28:48 -0800984 std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>&
James Feist899e17f2019-09-13 11:46:29 -0700985 ifaces = inventory[name];
986 for (auto& iface : ifaces)
987 {
James Feist02d2b932020-02-06 16:28:48 -0800988 auto sharedPtr = iface.lock();
989 if (!sharedPtr)
990 {
991 continue; // was already deleted elsewhere
992 }
993 objServer.remove_interface(sharedPtr);
James Feist899e17f2019-09-13 11:46:29 -0700994 }
995 ifaces.clear();
996 systemConfiguration.erase(item.key());
997 logDeviceRemoved(item.value());
998 }
999
James Feist8f2710a2018-05-09 17:18:55 -07001000 nlohmann::json newConfiguration = systemConfiguration;
James Feist4131aea2018-03-09 09:47:30 -08001001 for (auto it = newConfiguration.begin();
1002 it != newConfiguration.end();)
1003 {
1004 auto findKey = oldConfiguration.find(it.key());
1005 if (findKey != oldConfiguration.end())
1006 {
1007 it = newConfiguration.erase(it);
1008 }
1009 else
1010 {
1011 it++;
1012 }
1013 }
James Feist899e17f2019-09-13 11:46:29 -07001014 for (const auto& item : newConfiguration.items())
1015 {
1016 logDeviceAdded(item.value());
1017 }
1018
James Feist2539ccd2020-05-01 16:15:08 -07001019 inProgress = false;
1020
Jonathan Doman6d649822021-05-05 16:53:04 -07001021 io.post([count, newConfiguration, &systemConfiguration,
1022 &objServer]() {
James Feist8f2710a2018-05-09 17:18:55 -07001023 loadOverlays(newConfiguration);
James Feistce4367c2018-10-16 09:19:57 -07001024
Jonathan Doman6d649822021-05-05 16:53:04 -07001025 io.post([&systemConfiguration]() {
James Feistbb43d022018-06-12 15:44:33 -07001026 if (!writeJsonFiles(systemConfiguration))
1027 {
1028 std::cerr << "Error writing json files\n";
1029 }
1030 });
Jonathan Doman6d649822021-05-05 16:53:04 -07001031 io.post([count, newConfiguration, &systemConfiguration,
1032 &objServer]() {
James Feist97a63f12018-05-17 13:50:57 -07001033 postToDbus(newConfiguration, systemConfiguration,
1034 objServer);
James Feist899e17f2019-09-13 11:46:29 -07001035 if (count != instance)
James Feist1df06a42019-04-11 14:23:04 -07001036 {
James Feist899e17f2019-09-13 11:46:29 -07001037 return;
James Feist1df06a42019-04-11 14:23:04 -07001038 }
James Feist899e17f2019-09-13 11:46:29 -07001039 startRemovedTimer(timer, systemConfiguration);
James Feist8f2710a2018-05-09 17:18:55 -07001040 });
1041 });
1042 });
1043 perfScan->run();
1044 });
James Feist75fdeeb2018-02-20 14:26:16 -08001045}
1046
James Feist98132792019-07-09 13:29:09 -07001047int main()
James Feist75fdeeb2018-02-20 14:26:16 -08001048{
1049 // setup connection to dbus
Ed Tanous07d467b2021-02-23 14:48:37 -08001050 systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1051 systemBus->request_name("xyz.openbmc_project.EntityManager");
James Feist4131aea2018-03-09 09:47:30 -08001052
Ed Tanous07d467b2021-02-23 14:48:37 -08001053 sdbusplus::asio::object_server objServer(systemBus);
James Feistfd1264a2018-05-03 12:10:00 -07001054
James Feist8f2710a2018-05-09 17:18:55 -07001055 std::shared_ptr<sdbusplus::asio::dbus_interface> entityIface =
1056 objServer.add_interface("/xyz/openbmc_project/EntityManager",
1057 "xyz.openbmc_project.EntityManager");
James Feistfd1264a2018-05-03 12:10:00 -07001058
James Feist4131aea2018-03-09 09:47:30 -08001059 // to keep reference to the match / filter objects so they don't get
1060 // destroyed
James Feist8f2710a2018-05-09 17:18:55 -07001061
1062 nlohmann::json systemConfiguration = nlohmann::json::object();
1063
Brad Bishopc76af0f2020-12-04 13:50:23 -05001064 // We need a poke from DBus for static providers that create all their
1065 // objects prior to claiming a well-known name, and thus don't emit any
1066 // org.freedesktop.DBus.Properties signals. Similarly if a process exits
1067 // for any reason, expected or otherwise, we'll need a poke to remove
1068 // entities from DBus.
1069 sdbusplus::bus::match::match nameOwnerChangedMatch(
Ed Tanous07d467b2021-02-23 14:48:37 -08001070 static_cast<sdbusplus::bus::bus&>(*systemBus),
Brad Bishopc76af0f2020-12-04 13:50:23 -05001071 sdbusplus::bus::match::rules::nameOwnerChanged(),
1072 [&](sdbusplus::message::message&) {
1073 propertiesChangedCallback(systemConfiguration, objServer);
1074 });
Brad Bishop10a8c5f2020-12-07 21:40:07 -05001075 // We also need a poke from DBus when new interfaces are created or
1076 // destroyed.
1077 sdbusplus::bus::match::match interfacesAddedMatch(
Ed Tanous07d467b2021-02-23 14:48:37 -08001078 static_cast<sdbusplus::bus::bus&>(*systemBus),
Brad Bishop10a8c5f2020-12-07 21:40:07 -05001079 sdbusplus::bus::match::rules::interfacesAdded(),
1080 [&](sdbusplus::message::message&) {
1081 propertiesChangedCallback(systemConfiguration, objServer);
1082 });
1083 sdbusplus::bus::match::match interfacesRemovedMatch(
Ed Tanous07d467b2021-02-23 14:48:37 -08001084 static_cast<sdbusplus::bus::bus&>(*systemBus),
Brad Bishop10a8c5f2020-12-07 21:40:07 -05001085 sdbusplus::bus::match::rules::interfacesRemoved(),
1086 [&](sdbusplus::message::message&) {
1087 propertiesChangedCallback(systemConfiguration, objServer);
1088 });
Brad Bishopc76af0f2020-12-04 13:50:23 -05001089
James Feist4dc617b2020-05-01 09:54:47 -07001090 io.post(
1091 [&]() { propertiesChangedCallback(systemConfiguration, objServer); });
James Feist4131aea2018-03-09 09:47:30 -08001092
James Feistfd1264a2018-05-03 12:10:00 -07001093 entityIface->register_method("ReScan", [&]() {
James Feist4dc617b2020-05-01 09:54:47 -07001094 propertiesChangedCallback(systemConfiguration, objServer);
James Feist75fdeeb2018-02-20 14:26:16 -08001095 });
James Feist8f2710a2018-05-09 17:18:55 -07001096 entityIface->initialize();
1097
James Feist1df06a42019-04-11 14:23:04 -07001098 if (fwVersionIsSame())
1099 {
1100 if (std::filesystem::is_regular_file(currentConfiguration))
1101 {
1102 // this file could just be deleted, but it's nice for debug
1103 std::filesystem::create_directory(tempConfigDir);
1104 std::filesystem::remove(lastConfiguration);
1105 std::filesystem::copy(currentConfiguration, lastConfiguration);
1106 std::filesystem::remove(currentConfiguration);
1107
1108 std::ifstream jsonStream(lastConfiguration);
1109 if (jsonStream.good())
1110 {
1111 auto data = nlohmann::json::parse(jsonStream, nullptr, false);
1112 if (data.is_discarded())
1113 {
1114 std::cerr << "syntax error in " << lastConfiguration
1115 << "\n";
1116 }
1117 else
1118 {
1119 lastJson = std::move(data);
1120 }
1121 }
1122 else
1123 {
1124 std::cerr << "unable to open " << lastConfiguration << "\n";
1125 }
1126 }
1127 }
1128 else
1129 {
1130 // not an error, just logging at this level to make it in the journal
1131 std::cerr << "Clearing previous configuration\n";
1132 std::filesystem::remove(currentConfiguration);
1133 }
1134
1135 // some boards only show up after power is on, we want to not say they are
1136 // removed until the same state happens
Ed Tanous07d467b2021-02-23 14:48:37 -08001137 setupPowerMatch(systemBus);
James Feist1df06a42019-04-11 14:23:04 -07001138
James Feist1b2e2242018-01-30 13:45:19 -08001139 io.run();
James Feist3cb5fec2018-01-23 14:41:51 -08001140
1141 return 0;
James Feist75fdeeb2018-02-20 14:26:16 -08001142}