blob: a46806ad6676d87092d0c7a4cd4e67c9ef7344f1 [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>
James Feista465ccc2019-02-08 12:51:01 -080045constexpr const char* configurationDirectory = PACKAGE_DIR "configurations";
46constexpr const char* schemaDirectory = PACKAGE_DIR "configurations/schemas";
James Feist1df06a42019-04-11 14:23:04 -070047constexpr const char* tempConfigDir = "/tmp/configuration/";
48constexpr const char* lastConfiguration = "/tmp/configuration/last.json";
49constexpr const char* currentConfiguration = "/var/configuration/system.json";
James Feista465ccc2019-02-08 12:51:01 -080050constexpr const char* globalSchema = "global.json";
Ed Tanous07d467b2021-02-23 14:48:37 -080051constexpr const bool debug = false;
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 Feistd58879a2019-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 Feistd58879a2019-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 Feistd58879a2019-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 Feistd58879a2019-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 Feistd58879a2019-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{
James Feista465ccc2019-02-08 12:51:01 -0800283 for (auto& dictPair : dict.items())
James Feist1b2e2242018-01-30 13:45:19 -0800284 {
James Feist8f2710a2018-05-09 17:18:55 -0700285 auto type = dictPair.value().type();
286 bool array = false;
287 if (dictPair.value().type() == nlohmann::json::value_t::array)
288 {
289 array = true;
290 if (!dictPair.value().size())
291 {
292 continue;
293 }
294 type = dictPair.value()[0].type();
295 bool isLegal = true;
James Feista465ccc2019-02-08 12:51:01 -0800296 for (const auto& arrayItem : dictPair.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 {
306 std::cerr << "dbus format error" << dictPair.value() << "\n";
307 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 }
James Feist97a63f12018-05-17 13:50:57 -0700314 std::string key = jsonPointerPath + "/" + dictPair.key();
James Feistbb43d022018-06-12 15:44:33 -0700315 if (permission == sdbusplus::asio::PropertyPermission::readWrite)
316 {
317 // all setable numbers are doubles as it is difficult to always
318 // create a configuration file with all whole numbers as decimals
319 // i.e. 1.0
James Feistebcc26b2019-03-22 12:30:43 -0700320 if (array)
321 {
322 if (dictPair.value()[0].is_number())
323 {
324 type = nlohmann::json::value_t::number_float;
325 }
326 }
327 else if (dictPair.value().is_number())
James Feistbb43d022018-06-12 15:44:33 -0700328 {
329 type = nlohmann::json::value_t::number_float;
330 }
331 }
332
James Feist8f2710a2018-05-09 17:18:55 -0700333 switch (type)
James Feist1b2e2242018-01-30 13:45:19 -0800334 {
James Feist9eb0b582018-04-27 12:15:46 -0700335 case (nlohmann::json::value_t::boolean):
336 {
James Feist8f2710a2018-05-09 17:18:55 -0700337 if (array)
338 {
339 // todo: array of bool isn't detected correctly by
340 // sdbusplus, change it to numbers
341 addArrayToDbus<uint64_t>(dictPair.key(), dictPair.value(),
James Feistebcc26b2019-03-22 12:30:43 -0700342 iface.get(), permission,
343 systemConfiguration, key);
James Feist8f2710a2018-05-09 17:18:55 -0700344 }
James Feistbb43d022018-06-12 15:44:33 -0700345
James Feist97a63f12018-05-17 13:50:57 -0700346 else
347 {
James Feistbb43d022018-06-12 15:44:33 -0700348 addProperty(dictPair.key(), dictPair.value().get<bool>(),
James Feistc6248a52018-08-14 10:09:45 -0700349 iface.get(), systemConfiguration, key,
350 permission);
James Feist97a63f12018-05-17 13:50:57 -0700351 }
James Feist9eb0b582018-04-27 12:15:46 -0700352 break;
353 }
354 case (nlohmann::json::value_t::number_integer):
355 {
James Feist8f2710a2018-05-09 17:18:55 -0700356 if (array)
357 {
358 addArrayToDbus<int64_t>(dictPair.key(), dictPair.value(),
James Feistebcc26b2019-03-22 12:30:43 -0700359 iface.get(), permission,
360 systemConfiguration, key);
James Feist97a63f12018-05-17 13:50:57 -0700361 }
362 else
363 {
James Feistbb43d022018-06-12 15:44:33 -0700364 addProperty(dictPair.key(), dictPair.value().get<int64_t>(),
James Feistc6248a52018-08-14 10:09:45 -0700365 iface.get(), systemConfiguration, key,
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 {
374 addArrayToDbus<uint64_t>(dictPair.key(), dictPair.value(),
James Feistebcc26b2019-03-22 12:30:43 -0700375 iface.get(), permission,
376 systemConfiguration, key);
James Feist97a63f12018-05-17 13:50:57 -0700377 }
378 else
379 {
James Feistbb43d022018-06-12 15:44:33 -0700380 addProperty(dictPair.key(),
James Feistc6248a52018-08-14 10:09:45 -0700381 dictPair.value().get<uint64_t>(), iface.get(),
James Feistbb43d022018-06-12 15:44:33 -0700382 systemConfiguration, key,
383 sdbusplus::asio::PropertyPermission::readOnly);
James Feist97a63f12018-05-17 13:50:57 -0700384 }
James Feist9eb0b582018-04-27 12:15:46 -0700385 break;
386 }
387 case (nlohmann::json::value_t::number_float):
388 {
James Feist8f2710a2018-05-09 17:18:55 -0700389 if (array)
390 {
391 addArrayToDbus<double>(dictPair.key(), dictPair.value(),
James Feistebcc26b2019-03-22 12:30:43 -0700392 iface.get(), permission,
393 systemConfiguration, key);
James Feist8f2710a2018-05-09 17:18:55 -0700394 }
James Feistbb43d022018-06-12 15:44:33 -0700395
James Feist97a63f12018-05-17 13:50:57 -0700396 else
397 {
James Feistbb43d022018-06-12 15:44:33 -0700398 addProperty(dictPair.key(), dictPair.value().get<double>(),
James Feistc6248a52018-08-14 10:09:45 -0700399 iface.get(), systemConfiguration, key,
400 permission);
James Feist97a63f12018-05-17 13:50:57 -0700401 }
James Feist9eb0b582018-04-27 12:15:46 -0700402 break;
403 }
404 case (nlohmann::json::value_t::string):
405 {
James Feist8f2710a2018-05-09 17:18:55 -0700406 if (array)
407 {
James Feistebcc26b2019-03-22 12:30:43 -0700408 addArrayToDbus<std::string>(
409 dictPair.key(), dictPair.value(), iface.get(),
410 permission, systemConfiguration, key);
James Feist97a63f12018-05-17 13:50:57 -0700411 }
412 else
413 {
James Feistc6248a52018-08-14 10:09:45 -0700414 addProperty(
415 dictPair.key(), dictPair.value().get<std::string>(),
416 iface.get(), systemConfiguration, key, permission);
James Feist97a63f12018-05-17 13:50:57 -0700417 }
James Feist9eb0b582018-04-27 12:15:46 -0700418 break;
419 }
James Feist0eb40352019-04-09 14:44:04 -0700420 default:
421 {
James Feista218ddb2019-04-11 14:01:31 -0700422 std::cerr << "Unexpected json type in system configuration "
423 << dictPair.key() << ": "
424 << dictPair.value().type_name() << "\n";
James Feist0eb40352019-04-09 14:44:04 -0700425 break;
426 }
James Feist1b2e2242018-01-30 13:45:19 -0800427 }
428 }
James Feistc6248a52018-08-14 10:09:45 -0700429 if (permission == sdbusplus::asio::PropertyPermission::readWrite)
430 {
431 createDeleteObjectMethod(jsonPointerPath, iface, objServer,
432 systemConfiguration);
433 }
James Feist8f2710a2018-05-09 17:18:55 -0700434 iface->initialize();
James Feist1b2e2242018-01-30 13:45:19 -0800435}
436
James Feista465ccc2019-02-08 12:51:01 -0800437sdbusplus::asio::PropertyPermission getPermission(const std::string& interface)
James Feistc6248a52018-08-14 10:09:45 -0700438{
439 return std::find(settableInterfaces.begin(), settableInterfaces.end(),
440 interface) != settableInterfaces.end()
441 ? sdbusplus::asio::PropertyPermission::readWrite
442 : sdbusplus::asio::PropertyPermission::readOnly;
443}
444
James Feista465ccc2019-02-08 12:51:01 -0800445void createAddObjectMethod(const std::string& jsonPointerPath,
446 const std::string& path,
447 nlohmann::json& systemConfiguration,
James Feistd58879a2019-09-11 11:26:07 -0700448 sdbusplus::asio::object_server& objServer,
449 const std::string& board)
James Feist68500ff2018-08-08 15:40:42 -0700450{
James Feistd58879a2019-09-11 11:26:07 -0700451 std::shared_ptr<sdbusplus::asio::dbus_interface> iface = createInterface(
452 objServer, path, "xyz.openbmc_project.AddObject", board);
James Feist68500ff2018-08-08 15:40:42 -0700453
454 iface->register_method(
455 "AddObject",
456 [&systemConfiguration, &objServer,
James Feistd58879a2019-09-11 11:26:07 -0700457 jsonPointerPath{std::string(jsonPointerPath)}, path{std::string(path)},
458 board](const boost::container::flat_map<std::string, JsonVariantType>&
459 data) {
James Feist68500ff2018-08-08 15:40:42 -0700460 nlohmann::json::json_pointer ptr(jsonPointerPath);
James Feista465ccc2019-02-08 12:51:01 -0800461 nlohmann::json& base = systemConfiguration[ptr];
James Feist68500ff2018-08-08 15:40:42 -0700462 auto findExposes = base.find("Exposes");
463
464 if (findExposes == base.end())
465 {
466 throw std::invalid_argument("Entity must have children.");
467 }
468
469 // this will throw invalid-argument to sdbusplus if invalid json
470 nlohmann::json newData{};
James Feista465ccc2019-02-08 12:51:01 -0800471 for (const auto& item : data)
James Feist68500ff2018-08-08 15:40:42 -0700472 {
James Feista465ccc2019-02-08 12:51:01 -0800473 nlohmann::json& newJson = newData[item.first];
474 std::visit([&newJson](auto&& val) { newJson = std::move(val); },
475 item.second);
James Feist68500ff2018-08-08 15:40:42 -0700476 }
477
478 auto findName = newData.find("Name");
479 auto findType = newData.find("Type");
480 if (findName == newData.end() || findType == newData.end())
481 {
482 throw std::invalid_argument("AddObject missing Name or Type");
483 }
James Feista465ccc2019-02-08 12:51:01 -0800484 const std::string* type = findType->get_ptr<const std::string*>();
485 const std::string* name = findName->get_ptr<const std::string*>();
James Feist68500ff2018-08-08 15:40:42 -0700486 if (type == nullptr || name == nullptr)
487 {
488 throw std::invalid_argument("Type and Name must be a string.");
489 }
490
James Feist02d2b932020-02-06 16:28:48 -0800491 bool foundNull = false;
James Feist68500ff2018-08-08 15:40:42 -0700492 size_t lastIndex = 0;
493 // we add in the "exposes"
James Feist02d2b932020-02-06 16:28:48 -0800494 for (const auto& expose : *findExposes)
James Feist68500ff2018-08-08 15:40:42 -0700495 {
James Feist02d2b932020-02-06 16:28:48 -0800496 if (expose.is_null())
497 {
498 foundNull = true;
499 continue;
500 }
501
502 if (expose["Name"] == *name && expose["Type"] == *type)
James Feist68500ff2018-08-08 15:40:42 -0700503 {
504 throw std::invalid_argument(
505 "Field already in JSON, not adding");
506 }
James Feist02d2b932020-02-06 16:28:48 -0800507
508 if (foundNull)
509 {
510 continue;
511 }
512
James Feist68500ff2018-08-08 15:40:42 -0700513 lastIndex++;
514 }
515
516 std::ifstream schemaFile(std::string(schemaDirectory) + "/" +
517 *type + ".json");
518 // todo(james) we might want to also make a list of 'can add'
519 // interfaces but for now I think the assumption if there is a
520 // schema avaliable that it is allowed to update is fine
521 if (!schemaFile.good())
522 {
523 throw std::invalid_argument(
524 "No schema avaliable, cannot validate.");
525 }
526 nlohmann::json schema =
527 nlohmann::json::parse(schemaFile, nullptr, false);
528 if (schema.is_discarded())
529 {
530 std::cerr << "Schema not legal" << *type << ".json\n";
531 throw DBusInternalError();
532 }
533 if (!validateJson(schema, newData))
534 {
535 throw std::invalid_argument("Data does not match schema");
536 }
James Feist02d2b932020-02-06 16:28:48 -0800537 if (foundNull)
538 {
539 findExposes->at(lastIndex) = newData;
540 }
541 else
542 {
543 findExposes->push_back(newData);
544 }
James Feist68500ff2018-08-08 15:40:42 -0700545 if (!writeJsonFiles(systemConfiguration))
546 {
547 std::cerr << "Error writing json files\n";
548 throw DBusInternalError();
549 }
550 std::string dbusName = *name;
551
552 std::regex_replace(dbusName.begin(), dbusName.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800553 dbusName.end(), illegalDbusMemberRegex, "_");
James Feistd58879a2019-09-11 11:26:07 -0700554
555 std::shared_ptr<sdbusplus::asio::dbus_interface> interface =
556 createInterface(objServer, path + "/" + dbusName,
557 "xyz.openbmc_project.Configuration." + *type,
James Feist02d2b932020-02-06 16:28:48 -0800558 board, true);
James Feist68500ff2018-08-08 15:40:42 -0700559 // permission is read-write, as since we just created it, must be
560 // runtime modifiable
561 populateInterfaceFromJson(
562 systemConfiguration,
James Feist16a02f22019-05-13 15:21:37 -0700563 jsonPointerPath + "/Exposes/" + std::to_string(lastIndex),
James Feist98132792019-07-09 13:29:09 -0700564 interface, newData, objServer,
James Feist68500ff2018-08-08 15:40:42 -0700565 sdbusplus::asio::PropertyPermission::readWrite);
James Feist68500ff2018-08-08 15:40:42 -0700566 });
567 iface->initialize();
568}
569
James Feista465ccc2019-02-08 12:51:01 -0800570void postToDbus(const nlohmann::json& newConfiguration,
571 nlohmann::json& systemConfiguration,
572 sdbusplus::asio::object_server& objServer)
James Feist75fdeeb2018-02-20 14:26:16 -0800573
James Feist1b2e2242018-01-30 13:45:19 -0800574{
James Feist97a63f12018-05-17 13:50:57 -0700575 // iterate through boards
James Feista465ccc2019-02-08 12:51:01 -0800576 for (auto& boardPair : newConfiguration.items())
James Feist1b2e2242018-01-30 13:45:19 -0800577 {
James Feistf1b14142019-04-10 15:22:09 -0700578 std::string boardKey = boardPair.value()["Name"];
James Feistd58879a2019-09-11 11:26:07 -0700579 std::string boardKeyOrig = boardPair.value()["Name"];
James Feistf1b14142019-04-10 15:22:09 -0700580 std::string jsonPointerPath = "/" + boardPair.key();
James Feist97a63f12018-05-17 13:50:57 -0700581 // loop through newConfiguration, but use values from system
582 // configuration to be able to modify via dbus later
James Feistf1b14142019-04-10 15:22:09 -0700583 auto boardValues = systemConfiguration[boardPair.key()];
James Feistd63d18a2018-07-19 15:23:45 -0700584 auto findBoardType = boardValues.find("Type");
James Feist1b2e2242018-01-30 13:45:19 -0800585 std::string boardType;
586 if (findBoardType != boardValues.end() &&
587 findBoardType->type() == nlohmann::json::value_t::string)
588 {
589 boardType = findBoardType->get<std::string>();
590 std::regex_replace(boardType.begin(), boardType.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800591 boardType.end(), illegalDbusMemberRegex, "_");
James Feist1b2e2242018-01-30 13:45:19 -0800592 }
593 else
594 {
595 std::cerr << "Unable to find type for " << boardKey
596 << " reverting to Chassis.\n";
597 boardType = "Chassis";
598 }
James Feist11be6672018-04-06 14:05:32 -0700599 std::string boardtypeLower = boost::algorithm::to_lower_copy(boardType);
James Feist1b2e2242018-01-30 13:45:19 -0800600
601 std::regex_replace(boardKey.begin(), boardKey.begin(), boardKey.end(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800602 illegalDbusMemberRegex, "_");
603 std::string boardName = "/xyz/openbmc_project/inventory/system/";
604 boardName += boardtypeLower;
605 boardName += "/";
606 boardName += boardKey;
James Feist1b2e2242018-01-30 13:45:19 -0800607
James Feistd58879a2019-09-11 11:26:07 -0700608 std::shared_ptr<sdbusplus::asio::dbus_interface> inventoryIface =
609 createInterface(objServer, boardName,
610 "xyz.openbmc_project.Inventory.Item", boardKey);
James Feist68500ff2018-08-08 15:40:42 -0700611
James Feistd58879a2019-09-11 11:26:07 -0700612 std::shared_ptr<sdbusplus::asio::dbus_interface> boardIface =
613 createInterface(objServer, boardName,
614 "xyz.openbmc_project.Inventory.Item." + boardType,
615 boardKeyOrig);
James Feist11be6672018-04-06 14:05:32 -0700616
James Feist68500ff2018-08-08 15:40:42 -0700617 createAddObjectMethod(jsonPointerPath, boardName, systemConfiguration,
James Feistd58879a2019-09-11 11:26:07 -0700618 objServer, boardKeyOrig);
James Feist68500ff2018-08-08 15:40:42 -0700619
James Feist97a63f12018-05-17 13:50:57 -0700620 populateInterfaceFromJson(systemConfiguration, jsonPointerPath,
James Feistc6248a52018-08-14 10:09:45 -0700621 boardIface, boardValues, objServer);
James Feist97a63f12018-05-17 13:50:57 -0700622 jsonPointerPath += "/";
623 // iterate through board properties
James Feista465ccc2019-02-08 12:51:01 -0800624 for (auto& boardField : boardValues.items())
James Feist11be6672018-04-06 14:05:32 -0700625 {
626 if (boardField.value().type() == nlohmann::json::value_t::object)
627 {
James Feistd58879a2019-09-11 11:26:07 -0700628 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
629 createInterface(objServer, boardName, boardField.key(),
630 boardKeyOrig);
631
James Feistc6248a52018-08-14 10:09:45 -0700632 populateInterfaceFromJson(systemConfiguration,
633 jsonPointerPath + boardField.key(),
634 iface, boardField.value(), objServer);
James Feist11be6672018-04-06 14:05:32 -0700635 }
636 }
James Feist97a63f12018-05-17 13:50:57 -0700637
James Feist1e3e6982018-08-03 16:09:28 -0700638 auto exposes = boardValues.find("Exposes");
James Feist1b2e2242018-01-30 13:45:19 -0800639 if (exposes == boardValues.end())
640 {
641 continue;
642 }
James Feist97a63f12018-05-17 13:50:57 -0700643 // iterate through exposes
James Feist1e3e6982018-08-03 16:09:28 -0700644 jsonPointerPath += "Exposes/";
James Feist97a63f12018-05-17 13:50:57 -0700645
646 // store the board level pointer so we can modify it on the way down
647 std::string jsonPointerPathBoard = jsonPointerPath;
648 size_t exposesIndex = -1;
James Feista465ccc2019-02-08 12:51:01 -0800649 for (auto& item : *exposes)
James Feist1b2e2242018-01-30 13:45:19 -0800650 {
James Feist97a63f12018-05-17 13:50:57 -0700651 exposesIndex++;
652 jsonPointerPath = jsonPointerPathBoard;
653 jsonPointerPath += std::to_string(exposesIndex);
654
James Feistd63d18a2018-07-19 15:23:45 -0700655 auto findName = item.find("Name");
James Feist1b2e2242018-01-30 13:45:19 -0800656 if (findName == item.end())
657 {
658 std::cerr << "cannot find name in field " << item << "\n";
659 continue;
660 }
James Feist1e3e6982018-08-03 16:09:28 -0700661 auto findStatus = item.find("Status");
James Feist1b2e2242018-01-30 13:45:19 -0800662 // if status is not found it is assumed to be status = 'okay'
663 if (findStatus != item.end())
664 {
665 if (*findStatus == "disabled")
666 {
667 continue;
668 }
669 }
James Feistd63d18a2018-07-19 15:23:45 -0700670 auto findType = item.find("Type");
James Feist1b2e2242018-01-30 13:45:19 -0800671 std::string itemType;
672 if (findType != item.end())
673 {
674 itemType = findType->get<std::string>();
675 std::regex_replace(itemType.begin(), itemType.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800676 itemType.end(), illegalDbusPathRegex, "_");
James Feist1b2e2242018-01-30 13:45:19 -0800677 }
678 else
679 {
680 itemType = "unknown";
681 }
682 std::string itemName = findName->get<std::string>();
683 std::regex_replace(itemName.begin(), itemName.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800684 itemName.end(), illegalDbusMemberRegex, "_");
685 std::string ifacePath = boardName;
686 ifacePath += "/";
687 ifacePath += itemName;
James Feistc6248a52018-08-14 10:09:45 -0700688
James Feistd58879a2019-09-11 11:26:07 -0700689 std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface =
Ed Tanous07d467b2021-02-23 14:48:37 -0800690 createInterface(objServer, ifacePath,
James Feistd58879a2019-09-11 11:26:07 -0700691 "xyz.openbmc_project.Configuration." + itemType,
692 boardKeyOrig);
James Feist1b2e2242018-01-30 13:45:19 -0800693
James Feist97a63f12018-05-17 13:50:57 -0700694 populateInterfaceFromJson(systemConfiguration, jsonPointerPath,
James Feistc6248a52018-08-14 10:09:45 -0700695 itemIface, item, objServer,
696 getPermission(itemType));
James Feist1b2e2242018-01-30 13:45:19 -0800697
James Feista465ccc2019-02-08 12:51:01 -0800698 for (auto& objectPair : item.items())
James Feist1b2e2242018-01-30 13:45:19 -0800699 {
James Feist97a63f12018-05-17 13:50:57 -0700700 jsonPointerPath = jsonPointerPathBoard +
701 std::to_string(exposesIndex) + "/" +
702 objectPair.key();
James Feist1b2e2242018-01-30 13:45:19 -0800703 if (objectPair.value().type() ==
704 nlohmann::json::value_t::object)
705 {
James Feistd58879a2019-09-11 11:26:07 -0700706 std::shared_ptr<sdbusplus::asio::dbus_interface>
707 objectIface = createInterface(
Ed Tanous07d467b2021-02-23 14:48:37 -0800708 objServer, ifacePath,
James Feistd58879a2019-09-11 11:26:07 -0700709 "xyz.openbmc_project.Configuration." + itemType +
710 "." + objectPair.key(),
711 boardKeyOrig);
James Feist97a63f12018-05-17 13:50:57 -0700712
Adrian Ambrożewiczc789fca2020-05-14 15:50:05 +0200713 populateInterfaceFromJson(systemConfiguration,
714 jsonPointerPath, objectIface,
715 objectPair.value(), objServer,
716 getPermission(objectPair.key()));
James Feist1b2e2242018-01-30 13:45:19 -0800717 }
718 else if (objectPair.value().type() ==
719 nlohmann::json::value_t::array)
720 {
721 size_t index = 0;
James Feist8f2710a2018-05-09 17:18:55 -0700722 if (!objectPair.value().size())
James Feist1b2e2242018-01-30 13:45:19 -0800723 {
James Feist8f2710a2018-05-09 17:18:55 -0700724 continue;
725 }
726 bool isLegal = true;
727 auto type = objectPair.value()[0].type();
728 if (type != nlohmann::json::value_t::object)
729 {
730 continue;
731 }
732
733 // verify legal json
James Feista465ccc2019-02-08 12:51:01 -0800734 for (const auto& arrayItem : objectPair.value())
James Feist8f2710a2018-05-09 17:18:55 -0700735 {
736 if (arrayItem.type() != type)
James Feist1b2e2242018-01-30 13:45:19 -0800737 {
James Feist8f2710a2018-05-09 17:18:55 -0700738 isLegal = false;
James Feist1b2e2242018-01-30 13:45:19 -0800739 break;
740 }
James Feist8f2710a2018-05-09 17:18:55 -0700741 }
742 if (!isLegal)
743 {
744 std::cerr << "dbus format error" << objectPair.value()
745 << "\n";
746 break;
747 }
748
James Feista465ccc2019-02-08 12:51:01 -0800749 for (auto& arrayItem : objectPair.value())
James Feist8f2710a2018-05-09 17:18:55 -0700750 {
James Feist97a63f12018-05-17 13:50:57 -0700751
James Feistd58879a2019-09-11 11:26:07 -0700752 std::shared_ptr<sdbusplus::asio::dbus_interface>
753 objectIface = createInterface(
Ed Tanous07d467b2021-02-23 14:48:37 -0800754 objServer, ifacePath,
James Feistd58879a2019-09-11 11:26:07 -0700755 "xyz.openbmc_project.Configuration." +
756 itemType + "." + objectPair.key() +
757 std::to_string(index),
758 boardKeyOrig);
759
James Feistc6248a52018-08-14 10:09:45 -0700760 populateInterfaceFromJson(
761 systemConfiguration,
762 jsonPointerPath + "/" + std::to_string(index),
763 objectIface, arrayItem, objServer,
764 getPermission(objectPair.key()));
James Feistbb43d022018-06-12 15:44:33 -0700765 index++;
James Feist1b2e2242018-01-30 13:45:19 -0800766 }
767 }
768 }
769 }
770 }
771}
772
James Feist8f2710a2018-05-09 17:18:55 -0700773// reads json files out of the filesystem
James Feista465ccc2019-02-08 12:51:01 -0800774bool findJsonFiles(std::list<nlohmann::json>& configurations)
James Feist3cb5fec2018-01-23 14:41:51 -0800775{
776 // find configuration files
Ed Tanous072e25d2018-12-16 21:45:20 -0800777 std::vector<std::filesystem::path> jsonPaths;
Johnathan Mantey2015f752019-03-26 15:22:31 -0700778 if (!findFiles(std::filesystem::path(configurationDirectory), R"(.*\.json)",
779 jsonPaths))
James Feist3cb5fec2018-01-23 14:41:51 -0800780 {
781 std::cerr << "Unable to find any configuration files in "
James Feistb4383f42018-08-06 16:54:10 -0700782 << configurationDirectory << "\n";
James Feist75fdeeb2018-02-20 14:26:16 -0800783 return false;
James Feist3cb5fec2018-01-23 14:41:51 -0800784 }
James Feistb4383f42018-08-06 16:54:10 -0700785
786 std::ifstream schemaStream(std::string(schemaDirectory) + "/" +
787 globalSchema);
788 if (!schemaStream.good())
789 {
790 std::cerr
791 << "Cannot open schema file, cannot validate JSON, exiting\n\n";
792 std::exit(EXIT_FAILURE);
Ed Tanous072e25d2018-12-16 21:45:20 -0800793 return false;
James Feistb4383f42018-08-06 16:54:10 -0700794 }
795 nlohmann::json schema = nlohmann::json::parse(schemaStream, nullptr, false);
796 if (schema.is_discarded())
797 {
798 std::cerr
799 << "Illegal schema file detected, cannot validate JSON, exiting\n";
800 std::exit(EXIT_FAILURE);
Ed Tanous072e25d2018-12-16 21:45:20 -0800801 return false;
James Feistb4383f42018-08-06 16:54:10 -0700802 }
803
James Feista465ccc2019-02-08 12:51:01 -0800804 for (auto& jsonPath : jsonPaths)
James Feist3cb5fec2018-01-23 14:41:51 -0800805 {
806 std::ifstream jsonStream(jsonPath.c_str());
807 if (!jsonStream.good())
808 {
809 std::cerr << "unable to open " << jsonPath.string() << "\n";
810 continue;
811 }
812 auto data = nlohmann::json::parse(jsonStream, nullptr, false);
813 if (data.is_discarded())
814 {
815 std::cerr << "syntax error in " << jsonPath.string() << "\n";
816 continue;
817 }
James Feist8da99192019-01-24 08:20:16 -0800818 /*
819 * todo(james): reenable this once less things are in flight
820 *
James Feistb4383f42018-08-06 16:54:10 -0700821 if (!validateJson(schema, data))
822 {
823 std::cerr << "Error validating " << jsonPath.string() << "\n";
824 continue;
825 }
James Feist8da99192019-01-24 08:20:16 -0800826 */
James Feistb4383f42018-08-06 16:54:10 -0700827
James Feist3cb5fec2018-01-23 14:41:51 -0800828 if (data.type() == nlohmann::json::value_t::array)
829 {
James Feista465ccc2019-02-08 12:51:01 -0800830 for (auto& d : data)
James Feist3cb5fec2018-01-23 14:41:51 -0800831 {
832 configurations.emplace_back(d);
833 }
834 }
835 else
836 {
837 configurations.emplace_back(data);
838 }
839 }
Ed Tanous072e25d2018-12-16 21:45:20 -0800840 return true;
James Feist75fdeeb2018-02-20 14:26:16 -0800841}
James Feist3cb5fec2018-01-23 14:41:51 -0800842
James Feistb1728ca2020-04-30 15:40:55 -0700843void startRemovedTimer(boost::asio::steady_timer& timer,
James Feist1df06a42019-04-11 14:23:04 -0700844 nlohmann::json& systemConfiguration)
845{
846 static bool scannedPowerOff = false;
847 static bool scannedPowerOn = false;
848
James Feistfb00f392019-06-25 14:16:48 -0700849 if (systemConfiguration.empty() || lastJson.empty())
850 {
851 return; // not ready yet
852 }
James Feist1df06a42019-04-11 14:23:04 -0700853 if (scannedPowerOn)
854 {
855 return;
856 }
857
858 if (!isPowerOn() && scannedPowerOff)
859 {
860 return;
861 }
862
James Feistb1728ca2020-04-30 15:40:55 -0700863 timer.expires_after(std::chrono::seconds(10));
James Feist1a996582019-05-14 15:10:06 -0700864 timer.async_wait(
865 [&systemConfiguration](const boost::system::error_code& ec) {
866 if (ec == boost::asio::error::operation_aborted)
James Feist1df06a42019-04-11 14:23:04 -0700867 {
James Feist1a996582019-05-14 15:10:06 -0700868 // we were cancelled
869 return;
870 }
871
872 bool powerOff = !isPowerOn();
873 for (const auto& item : lastJson.items())
874 {
875 if (systemConfiguration.find(item.key()) ==
876 systemConfiguration.end())
James Feist1df06a42019-04-11 14:23:04 -0700877 {
James Feist1a996582019-05-14 15:10:06 -0700878 bool isDetectedPowerOn = false;
879 auto powerState = item.value().find("PowerState");
880 if (powerState != item.value().end())
James Feist1df06a42019-04-11 14:23:04 -0700881 {
James Feist1a996582019-05-14 15:10:06 -0700882 auto ptr = powerState->get_ptr<const std::string*>();
883 if (ptr)
James Feist1df06a42019-04-11 14:23:04 -0700884 {
James Feist1a996582019-05-14 15:10:06 -0700885 if (*ptr == "On" || *ptr == "BiosPost")
886 {
887 isDetectedPowerOn = true;
888 }
James Feist1df06a42019-04-11 14:23:04 -0700889 }
890 }
James Feist1a996582019-05-14 15:10:06 -0700891 if (powerOff && isDetectedPowerOn)
892 {
893 // power not on yet, don't know if it's there or not
894 continue;
895 }
896 if (!powerOff && scannedPowerOff && isDetectedPowerOn)
897 {
898 // already logged it when power was off
899 continue;
900 }
James Feist1df06a42019-04-11 14:23:04 -0700901
James Feist1a996582019-05-14 15:10:06 -0700902 logDeviceRemoved(item.value());
903 }
James Feist1df06a42019-04-11 14:23:04 -0700904 }
James Feist1a996582019-05-14 15:10:06 -0700905 scannedPowerOff = true;
906 if (!powerOff)
907 {
908 scannedPowerOn = true;
909 }
910 });
James Feist1df06a42019-04-11 14:23:04 -0700911}
912
James Feist8f2710a2018-05-09 17:18:55 -0700913// main properties changed entry
James Feist4dc617b2020-05-01 09:54:47 -0700914void propertiesChangedCallback(nlohmann::json& systemConfiguration,
915 sdbusplus::asio::object_server& objServer)
James Feist8f2710a2018-05-09 17:18:55 -0700916{
James Feist2539ccd2020-05-01 16:15:08 -0700917 static bool inProgress = false;
James Feistb1728ca2020-04-30 15:40:55 -0700918 static boost::asio::steady_timer timer(io);
James Feist899e17f2019-09-13 11:46:29 -0700919 static size_t instance = 0;
920 instance++;
921 size_t count = instance;
James Feist1df06a42019-04-11 14:23:04 -0700922
James Feistb1728ca2020-04-30 15:40:55 -0700923 timer.expires_after(std::chrono::seconds(5));
James Feist8f2710a2018-05-09 17:18:55 -0700924
925 // setup an async wait as we normally get flooded with new requests
James Feist4dc617b2020-05-01 09:54:47 -0700926 timer.async_wait([&systemConfiguration, &objServer,
James Feist899e17f2019-09-13 11:46:29 -0700927 count](const boost::system::error_code& ec) {
James Feist8f2710a2018-05-09 17:18:55 -0700928 if (ec == boost::asio::error::operation_aborted)
929 {
930 // we were cancelled
931 return;
932 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800933 if (ec)
James Feist8f2710a2018-05-09 17:18:55 -0700934 {
935 std::cerr << "async wait error " << ec << "\n";
936 return;
937 }
938
James Feist2539ccd2020-05-01 16:15:08 -0700939 if (inProgress)
940 {
941 propertiesChangedCallback(systemConfiguration, objServer);
942 return;
943 }
944 inProgress = true;
945
James Feist8f2710a2018-05-09 17:18:55 -0700946 nlohmann::json oldConfiguration = systemConfiguration;
James Feist899e17f2019-09-13 11:46:29 -0700947 auto missingConfigurations = std::make_shared<nlohmann::json>();
948 *missingConfigurations = systemConfiguration;
949
James Feist8f2710a2018-05-09 17:18:55 -0700950 std::list<nlohmann::json> configurations;
951 if (!findJsonFiles(configurations))
952 {
953 std::cerr << "cannot find json files\n";
James Feist2539ccd2020-05-01 16:15:08 -0700954 inProgress = false;
James Feist8f2710a2018-05-09 17:18:55 -0700955 return;
956 }
957
958 auto perfScan = std::make_shared<PerformScan>(
James Feist899e17f2019-09-13 11:46:29 -0700959 systemConfiguration, *missingConfigurations, configurations,
James Feist4dc617b2020-05-01 09:54:47 -0700960 objServer,
961 [&systemConfiguration, &objServer, count, oldConfiguration,
962 missingConfigurations]() {
James Feist899e17f2019-09-13 11:46:29 -0700963 // this is something that since ac has been applied to the bmc
964 // we saw, and we no longer see it
965 bool powerOff = !isPowerOn();
966 for (const auto& item : missingConfigurations->items())
967 {
968 bool isDetectedPowerOn = false;
969 auto powerState = item.value().find("PowerState");
970 if (powerState != item.value().end())
971 {
972 auto ptr = powerState->get_ptr<const std::string*>();
973 if (ptr)
974 {
975 if (*ptr == "On" || *ptr == "BiosPost")
976 {
977 isDetectedPowerOn = true;
978 }
979 }
980 }
981 if (powerOff && isDetectedPowerOn)
982 {
983 // power not on yet, don't know if it's there or not
984 continue;
985 }
986 std::string name = item.value()["Name"].get<std::string>();
James Feist02d2b932020-02-06 16:28:48 -0800987 std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>&
James Feist899e17f2019-09-13 11:46:29 -0700988 ifaces = inventory[name];
989 for (auto& iface : ifaces)
990 {
James Feist02d2b932020-02-06 16:28:48 -0800991 auto sharedPtr = iface.lock();
992 if (!sharedPtr)
993 {
994 continue; // was already deleted elsewhere
995 }
996 objServer.remove_interface(sharedPtr);
James Feist899e17f2019-09-13 11:46:29 -0700997 }
998 ifaces.clear();
999 systemConfiguration.erase(item.key());
1000 logDeviceRemoved(item.value());
1001 }
1002
James Feist8f2710a2018-05-09 17:18:55 -07001003 nlohmann::json newConfiguration = systemConfiguration;
James Feist4131aea2018-03-09 09:47:30 -08001004 for (auto it = newConfiguration.begin();
1005 it != newConfiguration.end();)
1006 {
1007 auto findKey = oldConfiguration.find(it.key());
1008 if (findKey != oldConfiguration.end())
1009 {
1010 it = newConfiguration.erase(it);
1011 }
1012 else
1013 {
1014 it++;
1015 }
1016 }
James Feist899e17f2019-09-13 11:46:29 -07001017 for (const auto& item : newConfiguration.items())
1018 {
1019 logDeviceAdded(item.value());
1020 }
1021
James Feist2539ccd2020-05-01 16:15:08 -07001022 inProgress = false;
1023
Jonathan Doman6d649822021-05-05 16:53:04 -07001024 io.post([count, newConfiguration, &systemConfiguration,
1025 &objServer]() {
James Feist8f2710a2018-05-09 17:18:55 -07001026 loadOverlays(newConfiguration);
James Feistce4367c2018-10-16 09:19:57 -07001027
Jonathan Doman6d649822021-05-05 16:53:04 -07001028 io.post([&systemConfiguration]() {
James Feistbb43d022018-06-12 15:44:33 -07001029 if (!writeJsonFiles(systemConfiguration))
1030 {
1031 std::cerr << "Error writing json files\n";
1032 }
1033 });
Jonathan Doman6d649822021-05-05 16:53:04 -07001034 io.post([count, newConfiguration, &systemConfiguration,
1035 &objServer]() {
James Feist97a63f12018-05-17 13:50:57 -07001036 postToDbus(newConfiguration, systemConfiguration,
1037 objServer);
James Feist899e17f2019-09-13 11:46:29 -07001038 if (count != instance)
James Feist1df06a42019-04-11 14:23:04 -07001039 {
James Feist899e17f2019-09-13 11:46:29 -07001040 return;
James Feist1df06a42019-04-11 14:23:04 -07001041 }
James Feist899e17f2019-09-13 11:46:29 -07001042 startRemovedTimer(timer, systemConfiguration);
James Feist8f2710a2018-05-09 17:18:55 -07001043 });
1044 });
1045 });
1046 perfScan->run();
1047 });
James Feist75fdeeb2018-02-20 14:26:16 -08001048}
1049
James Feist98132792019-07-09 13:29:09 -07001050int main()
James Feist75fdeeb2018-02-20 14:26:16 -08001051{
1052 // setup connection to dbus
Ed Tanous07d467b2021-02-23 14:48:37 -08001053 systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1054 systemBus->request_name("xyz.openbmc_project.EntityManager");
James Feist4131aea2018-03-09 09:47:30 -08001055
Ed Tanous07d467b2021-02-23 14:48:37 -08001056 sdbusplus::asio::object_server objServer(systemBus);
James Feistfd1264a2018-05-03 12:10:00 -07001057
James Feist8f2710a2018-05-09 17:18:55 -07001058 std::shared_ptr<sdbusplus::asio::dbus_interface> entityIface =
1059 objServer.add_interface("/xyz/openbmc_project/EntityManager",
1060 "xyz.openbmc_project.EntityManager");
James Feistfd1264a2018-05-03 12:10:00 -07001061
James Feist4131aea2018-03-09 09:47:30 -08001062 // to keep reference to the match / filter objects so they don't get
1063 // destroyed
James Feist8f2710a2018-05-09 17:18:55 -07001064
1065 nlohmann::json systemConfiguration = nlohmann::json::object();
1066
Brad Bishopc76af0f2020-12-04 13:50:23 -05001067 // We need a poke from DBus for static providers that create all their
1068 // objects prior to claiming a well-known name, and thus don't emit any
1069 // org.freedesktop.DBus.Properties signals. Similarly if a process exits
1070 // for any reason, expected or otherwise, we'll need a poke to remove
1071 // entities from DBus.
1072 sdbusplus::bus::match::match nameOwnerChangedMatch(
Ed Tanous07d467b2021-02-23 14:48:37 -08001073 static_cast<sdbusplus::bus::bus&>(*systemBus),
Brad Bishopc76af0f2020-12-04 13:50:23 -05001074 sdbusplus::bus::match::rules::nameOwnerChanged(),
1075 [&](sdbusplus::message::message&) {
1076 propertiesChangedCallback(systemConfiguration, objServer);
1077 });
Brad Bishop10a8c5f2020-12-07 21:40:07 -05001078 // We also need a poke from DBus when new interfaces are created or
1079 // destroyed.
1080 sdbusplus::bus::match::match interfacesAddedMatch(
Ed Tanous07d467b2021-02-23 14:48:37 -08001081 static_cast<sdbusplus::bus::bus&>(*systemBus),
Brad Bishop10a8c5f2020-12-07 21:40:07 -05001082 sdbusplus::bus::match::rules::interfacesAdded(),
1083 [&](sdbusplus::message::message&) {
1084 propertiesChangedCallback(systemConfiguration, objServer);
1085 });
1086 sdbusplus::bus::match::match interfacesRemovedMatch(
Ed Tanous07d467b2021-02-23 14:48:37 -08001087 static_cast<sdbusplus::bus::bus&>(*systemBus),
Brad Bishop10a8c5f2020-12-07 21:40:07 -05001088 sdbusplus::bus::match::rules::interfacesRemoved(),
1089 [&](sdbusplus::message::message&) {
1090 propertiesChangedCallback(systemConfiguration, objServer);
1091 });
Brad Bishopc76af0f2020-12-04 13:50:23 -05001092
James Feist4dc617b2020-05-01 09:54:47 -07001093 io.post(
1094 [&]() { propertiesChangedCallback(systemConfiguration, objServer); });
James Feist4131aea2018-03-09 09:47:30 -08001095
James Feistfd1264a2018-05-03 12:10:00 -07001096 entityIface->register_method("ReScan", [&]() {
James Feist4dc617b2020-05-01 09:54:47 -07001097 propertiesChangedCallback(systemConfiguration, objServer);
James Feist75fdeeb2018-02-20 14:26:16 -08001098 });
James Feist8f2710a2018-05-09 17:18:55 -07001099 entityIface->initialize();
1100
James Feist1df06a42019-04-11 14:23:04 -07001101 if (fwVersionIsSame())
1102 {
1103 if (std::filesystem::is_regular_file(currentConfiguration))
1104 {
1105 // this file could just be deleted, but it's nice for debug
1106 std::filesystem::create_directory(tempConfigDir);
1107 std::filesystem::remove(lastConfiguration);
1108 std::filesystem::copy(currentConfiguration, lastConfiguration);
1109 std::filesystem::remove(currentConfiguration);
1110
1111 std::ifstream jsonStream(lastConfiguration);
1112 if (jsonStream.good())
1113 {
1114 auto data = nlohmann::json::parse(jsonStream, nullptr, false);
1115 if (data.is_discarded())
1116 {
1117 std::cerr << "syntax error in " << lastConfiguration
1118 << "\n";
1119 }
1120 else
1121 {
1122 lastJson = std::move(data);
1123 }
1124 }
1125 else
1126 {
1127 std::cerr << "unable to open " << lastConfiguration << "\n";
1128 }
1129 }
1130 }
1131 else
1132 {
1133 // not an error, just logging at this level to make it in the journal
1134 std::cerr << "Clearing previous configuration\n";
1135 std::filesystem::remove(currentConfiguration);
1136 }
1137
1138 // some boards only show up after power is on, we want to not say they are
1139 // removed until the same state happens
Ed Tanous07d467b2021-02-23 14:48:37 -08001140 setupPowerMatch(systemBus);
James Feist1df06a42019-04-11 14:23:04 -07001141
James Feist1b2e2242018-01-30 13:45:19 -08001142 io.run();
James Feist3cb5fec2018-01-23 14:41:51 -08001143
1144 return 0;
James Feist75fdeeb2018-02-20 14:26:16 -08001145}