blob: b36610b23c334c8181a1ecb9f84938e6a614b8be [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>
James Feista465ccc2019-02-08 12:51:01 -080042#include <regex>
James Feista465ccc2019-02-08 12:51:01 -080043#include <variant>
James Feista465ccc2019-02-08 12:51:01 -080044constexpr const char* configurationDirectory = PACKAGE_DIR "configurations";
45constexpr const char* schemaDirectory = PACKAGE_DIR "configurations/schemas";
James Feist1df06a42019-04-11 14:23:04 -070046constexpr const char* tempConfigDir = "/tmp/configuration/";
47constexpr const char* lastConfiguration = "/tmp/configuration/last.json";
48constexpr const char* currentConfiguration = "/var/configuration/system.json";
James Feista465ccc2019-02-08 12:51:01 -080049constexpr const char* globalSchema = "global.json";
James Feist8f2710a2018-05-09 17:18:55 -070050constexpr const int32_t MAX_MAPPER_DEPTH = 0;
James Feist3cb5fec2018-01-23 14:41:51 -080051
James Feistf1b14142019-04-10 15:22:09 -070052constexpr const bool DEBUG = false;
53
James Feist3cb5fec2018-01-23 14:41:51 -080054struct cmp_str
55{
James Feista465ccc2019-02-08 12:51:01 -080056 bool operator()(const char* a, const char* b) const
James Feist3cb5fec2018-01-23 14:41:51 -080057 {
58 return std::strcmp(a, b) < 0;
59 }
60};
61
62// underscore T for collison with dbus c api
63enum class probe_type_codes
64{
65 FALSE_T,
66 TRUE_T,
67 AND,
68 OR,
James Feist6bd2a022018-03-13 12:30:58 -070069 FOUND,
70 MATCH_ONE
James Feist3cb5fec2018-01-23 14:41:51 -080071};
James Feista465ccc2019-02-08 12:51:01 -080072const static boost::container::flat_map<const char*, probe_type_codes, cmp_str>
James Feist3cb5fec2018-01-23 14:41:51 -080073 PROBE_TYPES{{{"FALSE", probe_type_codes::FALSE_T},
74 {"TRUE", probe_type_codes::TRUE_T},
75 {"AND", probe_type_codes::AND},
76 {"OR", probe_type_codes::OR},
James Feist6bd2a022018-03-13 12:30:58 -070077 {"FOUND", probe_type_codes::FOUND},
78 {"MATCH_ONE", probe_type_codes::MATCH_ONE}}};
James Feist3cb5fec2018-01-23 14:41:51 -080079
Adrian Ambrożewiczc789fca2020-05-14 15:50:05 +020080static constexpr std::array<const char*, 6> settableInterfaces = {
81 "FanProfile", "Pid", "Pid.Zone", "Stepwise", "Thresholds", "Polling"};
James Feist68500ff2018-08-08 15:40:42 -070082using JsonVariantType =
James Feist338b8a72019-03-01 10:16:45 -080083 std::variant<std::vector<std::string>, std::vector<double>, std::string,
84 int64_t, uint64_t, double, int32_t, uint32_t, int16_t,
85 uint16_t, uint8_t, bool>;
James Feist3cb5fec2018-01-23 14:41:51 -080086using GetSubTreeType = std::vector<
87 std::pair<std::string,
88 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
89
90using ManagedObjectType = boost::container::flat_map<
James Feist8f2710a2018-05-09 17:18:55 -070091 sdbusplus::message::object_path,
James Feist3cb5fec2018-01-23 14:41:51 -080092 boost::container::flat_map<
93 std::string,
James Feist8f2710a2018-05-09 17:18:55 -070094 boost::container::flat_map<std::string, BasicVariantType>>>;
James Feist3cb5fec2018-01-23 14:41:51 -080095
James Feistd58879a2019-09-11 11:26:07 -070096// store reference to all interfaces so we can destroy them later
97boost::container::flat_map<
James Feist02d2b932020-02-06 16:28:48 -080098 std::string, std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>>
James Feistd58879a2019-09-11 11:26:07 -070099 inventory;
100
James Feist3cb5fec2018-01-23 14:41:51 -0800101// todo: pass this through nicer
James Feist8f2710a2018-05-09 17:18:55 -0700102std::shared_ptr<sdbusplus::asio::connection> SYSTEM_BUS;
James Feist1df06a42019-04-11 14:23:04 -0700103static nlohmann::json lastJson;
James Feist3cb5fec2018-01-23 14:41:51 -0800104
James Feist02d2b932020-02-06 16:28:48 -0800105boost::asio::io_context io;
106
Johnathan Mantey2015f752019-03-26 15:22:31 -0700107const std::regex ILLEGAL_DBUS_PATH_REGEX("[^A-Za-z0-9_.]");
108const std::regex ILLEGAL_DBUS_MEMBER_REGEX("[^A-Za-z0-9_]");
James Feist1b2e2242018-01-30 13:45:19 -0800109
James Feist4dc617b2020-05-01 09:54:47 -0700110void registerCallback(nlohmann::json& systemConfiguration,
111 sdbusplus::asio::object_server& objServer,
Matt Spinlere789bf12021-02-24 12:33:01 -0600112 const std::string& path);
James Feist75fdeeb2018-02-20 14:26:16 -0800113
James Feistd58879a2019-09-11 11:26:07 -0700114static std::shared_ptr<sdbusplus::asio::dbus_interface>
115 createInterface(sdbusplus::asio::object_server& objServer,
116 const std::string& path, const std::string& interface,
James Feist02d2b932020-02-06 16:28:48 -0800117 const std::string& parent, bool checkNull = false)
James Feistd58879a2019-09-11 11:26:07 -0700118{
James Feist02d2b932020-02-06 16:28:48 -0800119 // on first add we have no reason to check for null before add, as there
120 // won't be any. For dynamically added interfaces, we check for null so that
121 // a constant delete/add will not create a memory leak
122
123 auto ptr = objServer.add_interface(path, interface);
124 auto& dataVector = inventory[parent];
125 if (checkNull)
126 {
127 auto it = std::find_if(dataVector.begin(), dataVector.end(),
128 [](const auto& p) { return p.expired(); });
129 if (it != dataVector.end())
130 {
131 *it = ptr;
132 return ptr;
133 }
134 }
135 dataVector.emplace_back(ptr);
136 return ptr;
James Feistd58879a2019-09-11 11:26:07 -0700137}
138
James Feistb1728ca2020-04-30 15:40:55 -0700139void getInterfaces(
140 const std::tuple<std::string, std::string, std::string>& call,
141 const std::vector<std::shared_ptr<PerformProbe>>& probeVector,
142 std::shared_ptr<PerformScan> scan, size_t retries = 5)
143{
144 if (!retries)
145 {
146 std::cerr << "retries exhausted on " << std::get<0>(call) << " "
147 << std::get<1>(call) << " " << std::get<2>(call) << "\n";
148 return;
149 }
150
151 SYSTEM_BUS->async_method_call(
152 [call, scan, probeVector, retries](
153 boost::system::error_code& errc,
154 const boost::container::flat_map<std::string, BasicVariantType>&
155 resp) {
156 if (errc)
157 {
158 std::cerr << "error calling getall on " << std::get<0>(call)
159 << " " << std::get<1>(call) << " "
160 << std::get<2>(call) << "\n";
161
162 std::shared_ptr<boost::asio::steady_timer> timer =
163 std::make_shared<boost::asio::steady_timer>(io);
164 timer->expires_after(std::chrono::seconds(2));
165
166 timer->async_wait([timer, call, scan, probeVector,
167 retries](const boost::system::error_code&) {
168 getInterfaces(call, probeVector, scan, retries - 1);
169 });
170 return;
171 }
172
Matt Spinlere789bf12021-02-24 12:33:01 -0600173 scan->dbusProbeObjects[std::get<1>(call)][std::get<2>(call)] = resp;
James Feistb1728ca2020-04-30 15:40:55 -0700174 },
175 std::get<0>(call), std::get<1>(call), "org.freedesktop.DBus.Properties",
176 "GetAll", std::get<2>(call));
177
178 if constexpr (DEBUG)
179 {
180 std::cerr << __func__ << " " << __LINE__ << "\n";
181 }
182}
183
Matt Spinlere789bf12021-02-24 12:33:01 -0600184// Populates scan->dbusProbeObjects with all interfaces and properties
185// for the paths that own the interfaces passed in.
James Feistb1728ca2020-04-30 15:40:55 -0700186void findDbusObjects(std::vector<std::shared_ptr<PerformProbe>>&& probeVector,
187 boost::container::flat_set<std::string>&& interfaces,
James Feist32d1f0a2020-09-10 14:49:25 -0700188 std::shared_ptr<PerformScan> scan, size_t retries = 5)
James Feist3cb5fec2018-01-23 14:41:51 -0800189{
Matt Spinlere789bf12021-02-24 12:33:01 -0600190 // Filter out interfaces already obtained.
191 for (const auto& [path, probeInterfaces] : scan->dbusProbeObjects)
James Feist733f7652019-11-13 14:32:29 -0800192 {
Matt Spinlere789bf12021-02-24 12:33:01 -0600193 for (const auto& [interface, _] : probeInterfaces)
194 {
195 interfaces.erase(interface);
196 }
James Feist733f7652019-11-13 14:32:29 -0800197 }
198 if (interfaces.empty())
199 {
200 return;
201 }
202
James Feist3cb5fec2018-01-23 14:41:51 -0800203 // find all connections in the mapper that expose a specific type
James Feistb1728ca2020-04-30 15:40:55 -0700204 SYSTEM_BUS->async_method_call(
Matt Spinler4f328892021-02-24 13:19:14 -0600205 [interfaces, probeVector{std::move(probeVector)}, scan,
206 retries](boost::system::error_code& ec,
207 const GetSubTreeType& interfaceSubtree) mutable {
James Feistb1728ca2020-04-30 15:40:55 -0700208 boost::container::flat_set<
209 std::tuple<std::string, std::string, std::string>>
210 interfaceConnections;
James Feist8f2710a2018-05-09 17:18:55 -0700211 if (ec)
James Feist494155a2018-03-14 16:23:24 -0700212 {
James Feist0de40152018-07-25 11:56:12 -0700213 if (ec.value() == ENOENT)
James Feist8f2710a2018-05-09 17:18:55 -0700214 {
James Feist0de40152018-07-25 11:56:12 -0700215 return; // wasn't found by mapper
James Feist8f2710a2018-05-09 17:18:55 -0700216 }
James Feist0de40152018-07-25 11:56:12 -0700217 std::cerr << "Error communicating to mapper.\n";
218
James Feist32d1f0a2020-09-10 14:49:25 -0700219 if (!retries)
220 {
221 // if we can't communicate to the mapper something is very
222 // wrong
223 std::exit(EXIT_FAILURE);
224 }
225 std::shared_ptr<boost::asio::steady_timer> timer =
226 std::make_shared<boost::asio::steady_timer>(io);
227 timer->expires_after(std::chrono::seconds(10));
228
229 timer->async_wait(
230 [timer, interfaces{std::move(interfaces)}, scan,
231 probeVector{std::move(probeVector)},
232 retries](const boost::system::error_code&) mutable {
233 findDbusObjects(std::move(probeVector),
234 std::move(interfaces), scan,
235 retries - 1);
236 });
237 return;
James Feist494155a2018-03-14 16:23:24 -0700238 }
James Feist787c3c32019-11-07 14:42:58 -0800239
James Feistb1728ca2020-04-30 15:40:55 -0700240 for (const auto& [path, object] : interfaceSubtree)
James Feist3cb5fec2018-01-23 14:41:51 -0800241 {
James Feistb1728ca2020-04-30 15:40:55 -0700242 for (const auto& [busname, ifaces] : object)
James Feist8f2710a2018-05-09 17:18:55 -0700243 {
James Feistb1728ca2020-04-30 15:40:55 -0700244 for (const std::string& iface : ifaces)
245 {
Matt Spinlere789bf12021-02-24 12:33:01 -0600246 // The 3 default org.freedeskstop interfaces (Peer,
247 // Introspectable, and Properties) are returned by
248 // the mapper but don't have properties, so don't bother
249 // with the GetAll call to save some cycles.
250 if (!boost::algorithm::starts_with(iface,
251 "org.freedesktop"))
James Feistb1728ca2020-04-30 15:40:55 -0700252 {
253 interfaceConnections.emplace(busname, path, iface);
254 }
255 }
James Feist8f2710a2018-05-09 17:18:55 -0700256 }
Matt Spinlere789bf12021-02-24 12:33:01 -0600257
258 // Get a PropertiesChanged callback for all
259 // interfaces on this path.
260 registerCallback(scan->_systemConfiguration, scan->objServer,
261 path);
James Feist3cb5fec2018-01-23 14:41:51 -0800262 }
James Feist787c3c32019-11-07 14:42:58 -0800263
James Feist63845bf2019-01-24 12:19:51 -0800264 if (interfaceConnections.empty())
265 {
James Feist63845bf2019-01-24 12:19:51 -0800266 return;
267 }
James Feist787c3c32019-11-07 14:42:58 -0800268
James Feistb1728ca2020-04-30 15:40:55 -0700269 for (const auto& call : interfaceConnections)
270 {
271 getInterfaces(call, probeVector, scan);
James Feist8f2710a2018-05-09 17:18:55 -0700272 }
273 },
274 "xyz.openbmc_project.ObjectMapper",
275 "/xyz/openbmc_project/object_mapper",
James Feist5131ac22018-10-25 12:22:23 -0700276 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", MAX_MAPPER_DEPTH,
James Feist787c3c32019-11-07 14:42:58 -0800277 interfaces);
James Feistb1728ca2020-04-30 15:40:55 -0700278
279 if constexpr (DEBUG)
280 {
281 std::cerr << __func__ << " " << __LINE__ << "\n";
282 }
James Feist3cb5fec2018-01-23 14:41:51 -0800283}
James Feistb1728ca2020-04-30 15:40:55 -0700284
James Feist8f2710a2018-05-09 17:18:55 -0700285// probes dbus interface dictionary for a key with a value that matches a regex
Matt Spinlere789bf12021-02-24 12:33:01 -0600286// When an interface passes a probe, also save its D-Bus path with it.
James Feist08a5b172019-08-28 14:47:47 -0700287bool probeDbus(const std::string& interface,
288 const std::map<std::string, nlohmann::json>& matches,
James Feist733f7652019-11-13 14:32:29 -0800289 FoundDeviceT& devices, std::shared_ptr<PerformScan> scan,
290 bool& foundProbe)
James Feist3cb5fec2018-01-23 14:41:51 -0800291{
James Feist3cb5fec2018-01-23 14:41:51 -0800292 bool foundMatch = false;
Matt Spinlere789bf12021-02-24 12:33:01 -0600293 foundProbe = false;
294
295 for (const auto& [path, interfaces] : scan->dbusProbeObjects)
James Feist3cb5fec2018-01-23 14:41:51 -0800296 {
Matt Spinlere789bf12021-02-24 12:33:01 -0600297 auto it = interfaces.find(interface);
298 if (it == interfaces.end())
James Feist3cb5fec2018-01-23 14:41:51 -0800299 {
Matt Spinlere789bf12021-02-24 12:33:01 -0600300 continue;
301 }
302
303 foundProbe = true;
304
305 bool deviceMatches = true;
306 const boost::container::flat_map<std::string, BasicVariantType>&
307 properties = it->second;
308
309 for (const auto& [matchProp, matchJSON] : matches)
310 {
311 auto deviceValue = properties.find(matchProp);
312 if (deviceValue != properties.end())
James Feist3cb5fec2018-01-23 14:41:51 -0800313 {
Jonathan Doman395c6d42021-05-04 19:25:10 -0700314 deviceMatches =
315 deviceMatches && matchProbe(matchJSON, deviceValue->second);
James Feist3cb5fec2018-01-23 14:41:51 -0800316 }
317 else
318 {
Matt Spinlere789bf12021-02-24 12:33:01 -0600319 // Move on to the next DBus path
James Feist3cb5fec2018-01-23 14:41:51 -0800320 deviceMatches = false;
321 break;
322 }
323 }
324 if (deviceMatches)
325 {
Matt Spinlere789bf12021-02-24 12:33:01 -0600326 if constexpr (DEBUG)
327 {
328 std::cerr << "probeDBus: Found probe match on " << path << " "
329 << interface << "\n";
330 }
331 devices.emplace_back(properties, path);
James Feist3cb5fec2018-01-23 14:41:51 -0800332 foundMatch = true;
James Feist3cb5fec2018-01-23 14:41:51 -0800333 }
334 }
335 return foundMatch;
336}
337
338// default probe entry point, iterates a list looking for specific types to
339// call specific probe functions
Matt Spinlere789bf12021-02-24 12:33:01 -0600340bool probe(const std::vector<std::string>& probeCommand,
341 std::shared_ptr<PerformScan> scan, FoundDeviceT& foundDevs)
James Feist3cb5fec2018-01-23 14:41:51 -0800342{
343 const static std::regex command(R"(\((.*)\))");
344 std::smatch match;
345 bool ret = false;
James Feist6bd2a022018-03-13 12:30:58 -0700346 bool matchOne = false;
James Feist3cb5fec2018-01-23 14:41:51 -0800347 bool cur = true;
348 probe_type_codes lastCommand = probe_type_codes::FALSE_T;
James Feist54a0dca2019-06-26 10:34:54 -0700349 bool first = true;
James Feist3cb5fec2018-01-23 14:41:51 -0800350
James Feista465ccc2019-02-08 12:51:01 -0800351 for (auto& probe : probeCommand)
James Feist3cb5fec2018-01-23 14:41:51 -0800352 {
353 bool foundProbe = false;
James Feista465ccc2019-02-08 12:51:01 -0800354 boost::container::flat_map<const char*, probe_type_codes,
James Feist3cb5fec2018-01-23 14:41:51 -0800355 cmp_str>::const_iterator probeType;
356
357 for (probeType = PROBE_TYPES.begin(); probeType != PROBE_TYPES.end();
Patrick Venture4b7a7452019-10-28 08:41:02 -0700358 ++probeType)
James Feist3cb5fec2018-01-23 14:41:51 -0800359 {
360 if (probe.find(probeType->first) != std::string::npos)
361 {
362 foundProbe = true;
363 break;
364 }
365 }
366 if (foundProbe)
367 {
368 switch (probeType->second)
369 {
James Feist9eb0b582018-04-27 12:15:46 -0700370 case probe_type_codes::FALSE_T:
James Feist3cb5fec2018-01-23 14:41:51 -0800371 {
James Feiste31e00a2019-07-24 10:45:43 -0700372 cur = false;
373 break;
James Feist3cb5fec2018-01-23 14:41:51 -0800374 }
James Feist9eb0b582018-04-27 12:15:46 -0700375 case probe_type_codes::TRUE_T:
376 {
James Feiste31e00a2019-07-24 10:45:43 -0700377 cur = true;
378 break;
James Feist9eb0b582018-04-27 12:15:46 -0700379 }
380 case probe_type_codes::MATCH_ONE:
381 {
382 // set current value to last, this probe type shouldn't
383 // affect the outcome
384 cur = ret;
385 matchOne = true;
386 break;
387 }
388 /*case probe_type_codes::AND:
389 break;
390 case probe_type_codes::OR:
391 break;
392 // these are no-ops until the last command switch
393 */
394 case probe_type_codes::FOUND:
395 {
396 if (!std::regex_search(probe, match, command))
397 {
James Feist0eb40352019-04-09 14:44:04 -0700398 std::cerr << "found probe syntax error " << probe
James Feist9eb0b582018-04-27 12:15:46 -0700399 << "\n";
400 return false;
401 }
402 std::string commandStr = *(match.begin() + 1);
403 boost::replace_all(commandStr, "'", "");
James Feist733f7652019-11-13 14:32:29 -0800404 cur = (std::find(scan->passedProbes.begin(),
405 scan->passedProbes.end(),
406 commandStr) != scan->passedProbes.end());
James Feist9eb0b582018-04-27 12:15:46 -0700407 break;
408 }
James Feist0eb40352019-04-09 14:44:04 -0700409 default:
410 {
411 break;
412 }
James Feist3cb5fec2018-01-23 14:41:51 -0800413 }
414 }
415 // look on dbus for object
416 else
417 {
418 if (!std::regex_search(probe, match, command))
419 {
James Feist0eb40352019-04-09 14:44:04 -0700420 std::cerr << "dbus probe syntax error " << probe << "\n";
James Feist3cb5fec2018-01-23 14:41:51 -0800421 return false;
422 }
423 std::string commandStr = *(match.begin() + 1);
424 // convert single ticks and single slashes into legal json
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700425 boost::replace_all(commandStr, "'", "\"");
James Feist3f8a2782018-02-12 09:24:42 -0800426 boost::replace_all(commandStr, R"(\)", R"(\\)");
James Feist3cb5fec2018-01-23 14:41:51 -0800427 auto json = nlohmann::json::parse(commandStr, nullptr, false);
428 if (json.is_discarded())
429 {
James Feist0eb40352019-04-09 14:44:04 -0700430 std::cerr << "dbus command syntax error " << commandStr << "\n";
James Feist3cb5fec2018-01-23 14:41:51 -0800431 return false;
432 }
433 // we can match any (string, variant) property. (string, string)
434 // does a regex
435 std::map<std::string, nlohmann::json> dbusProbeMap =
436 json.get<std::map<std::string, nlohmann::json>>();
437 auto findStart = probe.find("(");
438 if (findStart == std::string::npos)
439 {
440 return false;
441 }
442 std::string probeInterface = probe.substr(0, findStart);
James Feist733f7652019-11-13 14:32:29 -0800443 cur = probeDbus(probeInterface, dbusProbeMap, foundDevs, scan,
444 foundProbe);
James Feist3cb5fec2018-01-23 14:41:51 -0800445 }
446
447 // some functions like AND and OR only take affect after the
448 // fact
James Feist54a0dca2019-06-26 10:34:54 -0700449 if (lastCommand == probe_type_codes::AND)
James Feist3cb5fec2018-01-23 14:41:51 -0800450 {
James Feist54a0dca2019-06-26 10:34:54 -0700451 ret = cur && ret;
452 }
453 else if (lastCommand == probe_type_codes::OR)
454 {
455 ret = cur || ret;
456 }
457
458 if (first)
459 {
460 ret = cur;
461 first = false;
James Feist3cb5fec2018-01-23 14:41:51 -0800462 }
463 lastCommand = probeType != PROBE_TYPES.end()
464 ? probeType->second
465 : probe_type_codes::FALSE_T;
James Feist3cb5fec2018-01-23 14:41:51 -0800466 }
467
468 // probe passed, but empty device
James Feist3cb5fec2018-01-23 14:41:51 -0800469 if (ret && foundDevs.size() == 0)
470 {
James Feist08a5b172019-08-28 14:47:47 -0700471 foundDevs.emplace_back(
Matt Spinlere789bf12021-02-24 12:33:01 -0600472 boost::container::flat_map<std::string, BasicVariantType>{},
473 std::string{});
James Feist3cb5fec2018-01-23 14:41:51 -0800474 }
James Feist0eb40352019-04-09 14:44:04 -0700475 if (matchOne && ret)
James Feist6bd2a022018-03-13 12:30:58 -0700476 {
James Feist71f295f2019-06-20 13:35:12 -0700477 // match the last one
478 auto last = foundDevs.back();
James Feist0eb40352019-04-09 14:44:04 -0700479 foundDevs.clear();
James Feist71f295f2019-06-20 13:35:12 -0700480
481 foundDevs.emplace_back(std::move(last));
James Feist6bd2a022018-03-13 12:30:58 -0700482 }
James Feist3cb5fec2018-01-23 14:41:51 -0800483 return ret;
484}
485
Matt Spinlere789bf12021-02-24 12:33:01 -0600486PerformProbe::PerformProbe(
487 const std::vector<std::string>& probeCommand,
488 std::shared_ptr<PerformScan>& scanPtr,
489 std::function<void(FoundDeviceT&, const DBusProbeObjectT&)>&& callback) :
James Feist7d807752019-11-13 14:47:57 -0800490 _probeCommand(probeCommand),
491 scan(scanPtr), _callback(std::move(callback))
James Feist8c505da2020-05-28 10:06:33 -0700492{}
James Feist7d807752019-11-13 14:47:57 -0800493PerformProbe::~PerformProbe()
494{
495 FoundDeviceT foundDevs;
496 if (probe(_probeCommand, scan, foundDevs))
James Feist8f2710a2018-05-09 17:18:55 -0700497 {
Matt Spinlere789bf12021-02-24 12:33:01 -0600498 _callback(foundDevs, scan->dbusProbeObjects);
James Feist8f2710a2018-05-09 17:18:55 -0700499 }
James Feist7d807752019-11-13 14:47:57 -0800500}
James Feist8f2710a2018-05-09 17:18:55 -0700501
502// writes output files to persist data
James Feista465ccc2019-02-08 12:51:01 -0800503bool writeJsonFiles(const nlohmann::json& systemConfiguration)
James Feist1b2e2242018-01-30 13:45:19 -0800504{
James Feist1df06a42019-04-11 14:23:04 -0700505 std::filesystem::create_directory(configurationOutDir);
506 std::ofstream output(currentConfiguration);
James Feistbb43d022018-06-12 15:44:33 -0700507 if (!output.good())
508 {
509 return false;
510 }
James Feist1b2e2242018-01-30 13:45:19 -0800511 output << systemConfiguration.dump(4);
512 output.close();
James Feistbb43d022018-06-12 15:44:33 -0700513 return true;
James Feist8f2710a2018-05-09 17:18:55 -0700514}
James Feist1b2e2242018-01-30 13:45:19 -0800515
James Feist97a63f12018-05-17 13:50:57 -0700516template <typename JsonType>
James Feista465ccc2019-02-08 12:51:01 -0800517bool setJsonFromPointer(const std::string& ptrStr, const JsonType& value,
518 nlohmann::json& systemConfiguration)
James Feist97a63f12018-05-17 13:50:57 -0700519{
520 try
521 {
522 nlohmann::json::json_pointer ptr(ptrStr);
James Feista465ccc2019-02-08 12:51:01 -0800523 nlohmann::json& ref = systemConfiguration[ptr];
James Feist97a63f12018-05-17 13:50:57 -0700524 ref = value;
525 return true;
526 }
James Feist98132792019-07-09 13:29:09 -0700527 catch (const std::out_of_range&)
James Feist97a63f12018-05-17 13:50:57 -0700528 {
529 return false;
530 }
531}
James Feistbb43d022018-06-12 15:44:33 -0700532
James Feistebcc26b2019-03-22 12:30:43 -0700533// template function to add array as dbus property
534template <typename PropertyType>
535void addArrayToDbus(const std::string& name, const nlohmann::json& array,
536 sdbusplus::asio::dbus_interface* iface,
537 sdbusplus::asio::PropertyPermission permission,
538 nlohmann::json& systemConfiguration,
539 const std::string& jsonPointerString)
540{
541 std::vector<PropertyType> values;
542 for (const auto& property : array)
543 {
544 auto ptr = property.get_ptr<const PropertyType*>();
545 if (ptr != nullptr)
546 {
547 values.emplace_back(*ptr);
548 }
549 }
550
551 if (permission == sdbusplus::asio::PropertyPermission::readOnly)
552 {
553 iface->register_property(name, values);
554 }
555 else
556 {
557 iface->register_property(
558 name, values,
559 [&systemConfiguration,
560 jsonPointerString{std::string(jsonPointerString)}](
561 const std::vector<PropertyType>& newVal,
562 std::vector<PropertyType>& val) {
563 val = newVal;
564 if (!setJsonFromPointer(jsonPointerString, val,
565 systemConfiguration))
566 {
567 std::cerr << "error setting json field\n";
568 return -1;
569 }
570 if (!writeJsonFiles(systemConfiguration))
571 {
572 std::cerr << "error setting json file\n";
573 return -1;
574 }
575 return 1;
576 });
577 }
578}
579
James Feistbb43d022018-06-12 15:44:33 -0700580template <typename PropertyType>
James Feista465ccc2019-02-08 12:51:01 -0800581void addProperty(const std::string& propertyName, const PropertyType& value,
582 sdbusplus::asio::dbus_interface* iface,
583 nlohmann::json& systemConfiguration,
584 const std::string& jsonPointerString,
James Feistbb43d022018-06-12 15:44:33 -0700585 sdbusplus::asio::PropertyPermission permission)
586{
587 if (permission == sdbusplus::asio::PropertyPermission::readOnly)
588 {
589 iface->register_property(propertyName, value);
590 return;
591 }
James Feist68500ff2018-08-08 15:40:42 -0700592 iface->register_property(
593 propertyName, value,
594 [&systemConfiguration,
595 jsonPointerString{std::string(jsonPointerString)}](
James Feista465ccc2019-02-08 12:51:01 -0800596 const PropertyType& newVal, PropertyType& val) {
James Feist68500ff2018-08-08 15:40:42 -0700597 val = newVal;
598 if (!setJsonFromPointer(jsonPointerString, val,
599 systemConfiguration))
600 {
601 std::cerr << "error setting json field\n";
602 return -1;
603 }
James Feistc6248a52018-08-14 10:09:45 -0700604 if (!writeJsonFiles(systemConfiguration))
James Feist68500ff2018-08-08 15:40:42 -0700605 {
606 std::cerr << "error setting json file\n";
James Feistc6248a52018-08-14 10:09:45 -0700607 return -1;
608 }
609 return 1;
610 });
611}
612
613void createDeleteObjectMethod(
James Feista465ccc2019-02-08 12:51:01 -0800614 const std::string& jsonPointerPath,
615 const std::shared_ptr<sdbusplus::asio::dbus_interface>& iface,
616 sdbusplus::asio::object_server& objServer,
617 nlohmann::json& systemConfiguration)
James Feistc6248a52018-08-14 10:09:45 -0700618{
619 std::weak_ptr<sdbusplus::asio::dbus_interface> interface = iface;
620 iface->register_method(
621 "Delete", [&objServer, &systemConfiguration, interface,
622 jsonPointerPath{std::string(jsonPointerPath)}]() {
James Feist98132792019-07-09 13:29:09 -0700623 std::shared_ptr<sdbusplus::asio::dbus_interface> dbusInterface =
James Feistc6248a52018-08-14 10:09:45 -0700624 interface.lock();
James Feist98132792019-07-09 13:29:09 -0700625 if (!dbusInterface)
James Feistc6248a52018-08-14 10:09:45 -0700626 {
627 // this technically can't happen as the pointer is pointing to
628 // us
629 throw DBusInternalError();
630 }
631 nlohmann::json::json_pointer ptr(jsonPointerPath);
James Feistc6248a52018-08-14 10:09:45 -0700632 systemConfiguration[ptr] = nullptr;
633
James Feist02d2b932020-02-06 16:28:48 -0800634 // todo(james): dig through sdbusplus to find out why we can't
635 // delete it in a method call
636 io.post([&objServer, dbusInterface]() mutable {
637 objServer.remove_interface(dbusInterface);
638 });
639
James Feistc6248a52018-08-14 10:09:45 -0700640 if (!writeJsonFiles(systemConfiguration))
641 {
642 std::cerr << "error setting json file\n";
643 throw DBusInternalError();
James Feist68500ff2018-08-08 15:40:42 -0700644 }
James Feist68500ff2018-08-08 15:40:42 -0700645 });
James Feistbb43d022018-06-12 15:44:33 -0700646}
647
James Feist1b2e2242018-01-30 13:45:19 -0800648// adds simple json types to interface's properties
James Feistbb43d022018-06-12 15:44:33 -0700649void populateInterfaceFromJson(
James Feista465ccc2019-02-08 12:51:01 -0800650 nlohmann::json& systemConfiguration, const std::string& jsonPointerPath,
651 std::shared_ptr<sdbusplus::asio::dbus_interface>& iface,
652 nlohmann::json& dict, sdbusplus::asio::object_server& objServer,
James Feistbb43d022018-06-12 15:44:33 -0700653 sdbusplus::asio::PropertyPermission permission =
654 sdbusplus::asio::PropertyPermission::readOnly)
James Feist1b2e2242018-01-30 13:45:19 -0800655{
James Feista465ccc2019-02-08 12:51:01 -0800656 for (auto& dictPair : dict.items())
James Feist1b2e2242018-01-30 13:45:19 -0800657 {
James Feist8f2710a2018-05-09 17:18:55 -0700658 auto type = dictPair.value().type();
659 bool array = false;
660 if (dictPair.value().type() == nlohmann::json::value_t::array)
661 {
662 array = true;
663 if (!dictPair.value().size())
664 {
665 continue;
666 }
667 type = dictPair.value()[0].type();
668 bool isLegal = true;
James Feista465ccc2019-02-08 12:51:01 -0800669 for (const auto& arrayItem : dictPair.value())
James Feist8f2710a2018-05-09 17:18:55 -0700670 {
671 if (arrayItem.type() != type)
672 {
673 isLegal = false;
674 break;
675 }
676 }
677 if (!isLegal)
678 {
679 std::cerr << "dbus format error" << dictPair.value() << "\n";
680 continue;
681 }
James Feista218ddb2019-04-11 14:01:31 -0700682 }
683 if (type == nlohmann::json::value_t::object)
684 {
685 continue; // handled elsewhere
James Feist8f2710a2018-05-09 17:18:55 -0700686 }
James Feist97a63f12018-05-17 13:50:57 -0700687 std::string key = jsonPointerPath + "/" + dictPair.key();
James Feistbb43d022018-06-12 15:44:33 -0700688 if (permission == sdbusplus::asio::PropertyPermission::readWrite)
689 {
690 // all setable numbers are doubles as it is difficult to always
691 // create a configuration file with all whole numbers as decimals
692 // i.e. 1.0
James Feistebcc26b2019-03-22 12:30:43 -0700693 if (array)
694 {
695 if (dictPair.value()[0].is_number())
696 {
697 type = nlohmann::json::value_t::number_float;
698 }
699 }
700 else if (dictPair.value().is_number())
James Feistbb43d022018-06-12 15:44:33 -0700701 {
702 type = nlohmann::json::value_t::number_float;
703 }
704 }
705
James Feist8f2710a2018-05-09 17:18:55 -0700706 switch (type)
James Feist1b2e2242018-01-30 13:45:19 -0800707 {
James Feist9eb0b582018-04-27 12:15:46 -0700708 case (nlohmann::json::value_t::boolean):
709 {
James Feist8f2710a2018-05-09 17:18:55 -0700710 if (array)
711 {
712 // todo: array of bool isn't detected correctly by
713 // sdbusplus, change it to numbers
714 addArrayToDbus<uint64_t>(dictPair.key(), dictPair.value(),
James Feistebcc26b2019-03-22 12:30:43 -0700715 iface.get(), permission,
716 systemConfiguration, key);
James Feist8f2710a2018-05-09 17:18:55 -0700717 }
James Feistbb43d022018-06-12 15:44:33 -0700718
James Feist97a63f12018-05-17 13:50:57 -0700719 else
720 {
James Feistbb43d022018-06-12 15:44:33 -0700721 addProperty(dictPair.key(), dictPair.value().get<bool>(),
James Feistc6248a52018-08-14 10:09:45 -0700722 iface.get(), systemConfiguration, key,
723 permission);
James Feist97a63f12018-05-17 13:50:57 -0700724 }
James Feist9eb0b582018-04-27 12:15:46 -0700725 break;
726 }
727 case (nlohmann::json::value_t::number_integer):
728 {
James Feist8f2710a2018-05-09 17:18:55 -0700729 if (array)
730 {
731 addArrayToDbus<int64_t>(dictPair.key(), dictPair.value(),
James Feistebcc26b2019-03-22 12:30:43 -0700732 iface.get(), permission,
733 systemConfiguration, key);
James Feist97a63f12018-05-17 13:50:57 -0700734 }
735 else
736 {
James Feistbb43d022018-06-12 15:44:33 -0700737 addProperty(dictPair.key(), dictPair.value().get<int64_t>(),
James Feistc6248a52018-08-14 10:09:45 -0700738 iface.get(), systemConfiguration, key,
James Feistbb43d022018-06-12 15:44:33 -0700739 sdbusplus::asio::PropertyPermission::readOnly);
James Feist97a63f12018-05-17 13:50:57 -0700740 }
James Feist9eb0b582018-04-27 12:15:46 -0700741 break;
742 }
743 case (nlohmann::json::value_t::number_unsigned):
744 {
James Feist8f2710a2018-05-09 17:18:55 -0700745 if (array)
746 {
747 addArrayToDbus<uint64_t>(dictPair.key(), dictPair.value(),
James Feistebcc26b2019-03-22 12:30:43 -0700748 iface.get(), permission,
749 systemConfiguration, key);
James Feist97a63f12018-05-17 13:50:57 -0700750 }
751 else
752 {
James Feistbb43d022018-06-12 15:44:33 -0700753 addProperty(dictPair.key(),
James Feistc6248a52018-08-14 10:09:45 -0700754 dictPair.value().get<uint64_t>(), iface.get(),
James Feistbb43d022018-06-12 15:44:33 -0700755 systemConfiguration, key,
756 sdbusplus::asio::PropertyPermission::readOnly);
James Feist97a63f12018-05-17 13:50:57 -0700757 }
James Feist9eb0b582018-04-27 12:15:46 -0700758 break;
759 }
760 case (nlohmann::json::value_t::number_float):
761 {
James Feist8f2710a2018-05-09 17:18:55 -0700762 if (array)
763 {
764 addArrayToDbus<double>(dictPair.key(), dictPair.value(),
James Feistebcc26b2019-03-22 12:30:43 -0700765 iface.get(), permission,
766 systemConfiguration, key);
James Feist8f2710a2018-05-09 17:18:55 -0700767 }
James Feistbb43d022018-06-12 15:44:33 -0700768
James Feist97a63f12018-05-17 13:50:57 -0700769 else
770 {
James Feistbb43d022018-06-12 15:44:33 -0700771 addProperty(dictPair.key(), dictPair.value().get<double>(),
James Feistc6248a52018-08-14 10:09:45 -0700772 iface.get(), systemConfiguration, key,
773 permission);
James Feist97a63f12018-05-17 13:50:57 -0700774 }
James Feist9eb0b582018-04-27 12:15:46 -0700775 break;
776 }
777 case (nlohmann::json::value_t::string):
778 {
James Feist8f2710a2018-05-09 17:18:55 -0700779 if (array)
780 {
James Feistebcc26b2019-03-22 12:30:43 -0700781 addArrayToDbus<std::string>(
782 dictPair.key(), dictPair.value(), iface.get(),
783 permission, systemConfiguration, key);
James Feist97a63f12018-05-17 13:50:57 -0700784 }
785 else
786 {
James Feistc6248a52018-08-14 10:09:45 -0700787 addProperty(
788 dictPair.key(), dictPair.value().get<std::string>(),
789 iface.get(), systemConfiguration, key, permission);
James Feist97a63f12018-05-17 13:50:57 -0700790 }
James Feist9eb0b582018-04-27 12:15:46 -0700791 break;
792 }
James Feist0eb40352019-04-09 14:44:04 -0700793 default:
794 {
James Feista218ddb2019-04-11 14:01:31 -0700795 std::cerr << "Unexpected json type in system configuration "
796 << dictPair.key() << ": "
797 << dictPair.value().type_name() << "\n";
James Feist0eb40352019-04-09 14:44:04 -0700798 break;
799 }
James Feist1b2e2242018-01-30 13:45:19 -0800800 }
801 }
James Feistc6248a52018-08-14 10:09:45 -0700802 if (permission == sdbusplus::asio::PropertyPermission::readWrite)
803 {
804 createDeleteObjectMethod(jsonPointerPath, iface, objServer,
805 systemConfiguration);
806 }
James Feist8f2710a2018-05-09 17:18:55 -0700807 iface->initialize();
James Feist1b2e2242018-01-30 13:45:19 -0800808}
809
James Feista465ccc2019-02-08 12:51:01 -0800810sdbusplus::asio::PropertyPermission getPermission(const std::string& interface)
James Feistc6248a52018-08-14 10:09:45 -0700811{
812 return std::find(settableInterfaces.begin(), settableInterfaces.end(),
813 interface) != settableInterfaces.end()
814 ? sdbusplus::asio::PropertyPermission::readWrite
815 : sdbusplus::asio::PropertyPermission::readOnly;
816}
817
James Feista465ccc2019-02-08 12:51:01 -0800818void createAddObjectMethod(const std::string& jsonPointerPath,
819 const std::string& path,
820 nlohmann::json& systemConfiguration,
James Feistd58879a2019-09-11 11:26:07 -0700821 sdbusplus::asio::object_server& objServer,
822 const std::string& board)
James Feist68500ff2018-08-08 15:40:42 -0700823{
James Feistd58879a2019-09-11 11:26:07 -0700824 std::shared_ptr<sdbusplus::asio::dbus_interface> iface = createInterface(
825 objServer, path, "xyz.openbmc_project.AddObject", board);
James Feist68500ff2018-08-08 15:40:42 -0700826
827 iface->register_method(
828 "AddObject",
829 [&systemConfiguration, &objServer,
James Feistd58879a2019-09-11 11:26:07 -0700830 jsonPointerPath{std::string(jsonPointerPath)}, path{std::string(path)},
831 board](const boost::container::flat_map<std::string, JsonVariantType>&
832 data) {
James Feist68500ff2018-08-08 15:40:42 -0700833 nlohmann::json::json_pointer ptr(jsonPointerPath);
James Feista465ccc2019-02-08 12:51:01 -0800834 nlohmann::json& base = systemConfiguration[ptr];
James Feist68500ff2018-08-08 15:40:42 -0700835 auto findExposes = base.find("Exposes");
836
837 if (findExposes == base.end())
838 {
839 throw std::invalid_argument("Entity must have children.");
840 }
841
842 // this will throw invalid-argument to sdbusplus if invalid json
843 nlohmann::json newData{};
James Feista465ccc2019-02-08 12:51:01 -0800844 for (const auto& item : data)
James Feist68500ff2018-08-08 15:40:42 -0700845 {
James Feista465ccc2019-02-08 12:51:01 -0800846 nlohmann::json& newJson = newData[item.first];
847 std::visit([&newJson](auto&& val) { newJson = std::move(val); },
848 item.second);
James Feist68500ff2018-08-08 15:40:42 -0700849 }
850
851 auto findName = newData.find("Name");
852 auto findType = newData.find("Type");
853 if (findName == newData.end() || findType == newData.end())
854 {
855 throw std::invalid_argument("AddObject missing Name or Type");
856 }
James Feista465ccc2019-02-08 12:51:01 -0800857 const std::string* type = findType->get_ptr<const std::string*>();
858 const std::string* name = findName->get_ptr<const std::string*>();
James Feist68500ff2018-08-08 15:40:42 -0700859 if (type == nullptr || name == nullptr)
860 {
861 throw std::invalid_argument("Type and Name must be a string.");
862 }
863
James Feist02d2b932020-02-06 16:28:48 -0800864 bool foundNull = false;
James Feist68500ff2018-08-08 15:40:42 -0700865 size_t lastIndex = 0;
866 // we add in the "exposes"
James Feist02d2b932020-02-06 16:28:48 -0800867 for (const auto& expose : *findExposes)
James Feist68500ff2018-08-08 15:40:42 -0700868 {
James Feist02d2b932020-02-06 16:28:48 -0800869 if (expose.is_null())
870 {
871 foundNull = true;
872 continue;
873 }
874
875 if (expose["Name"] == *name && expose["Type"] == *type)
James Feist68500ff2018-08-08 15:40:42 -0700876 {
877 throw std::invalid_argument(
878 "Field already in JSON, not adding");
879 }
James Feist02d2b932020-02-06 16:28:48 -0800880
881 if (foundNull)
882 {
883 continue;
884 }
885
James Feist68500ff2018-08-08 15:40:42 -0700886 lastIndex++;
887 }
888
889 std::ifstream schemaFile(std::string(schemaDirectory) + "/" +
890 *type + ".json");
891 // todo(james) we might want to also make a list of 'can add'
892 // interfaces but for now I think the assumption if there is a
893 // schema avaliable that it is allowed to update is fine
894 if (!schemaFile.good())
895 {
896 throw std::invalid_argument(
897 "No schema avaliable, cannot validate.");
898 }
899 nlohmann::json schema =
900 nlohmann::json::parse(schemaFile, nullptr, false);
901 if (schema.is_discarded())
902 {
903 std::cerr << "Schema not legal" << *type << ".json\n";
904 throw DBusInternalError();
905 }
906 if (!validateJson(schema, newData))
907 {
908 throw std::invalid_argument("Data does not match schema");
909 }
James Feist02d2b932020-02-06 16:28:48 -0800910 if (foundNull)
911 {
912 findExposes->at(lastIndex) = newData;
913 }
914 else
915 {
916 findExposes->push_back(newData);
917 }
James Feist68500ff2018-08-08 15:40:42 -0700918 if (!writeJsonFiles(systemConfiguration))
919 {
920 std::cerr << "Error writing json files\n";
921 throw DBusInternalError();
922 }
923 std::string dbusName = *name;
924
925 std::regex_replace(dbusName.begin(), dbusName.begin(),
Johnathan Mantey2015f752019-03-26 15:22:31 -0700926 dbusName.end(), ILLEGAL_DBUS_MEMBER_REGEX, "_");
James Feistd58879a2019-09-11 11:26:07 -0700927
928 std::shared_ptr<sdbusplus::asio::dbus_interface> interface =
929 createInterface(objServer, path + "/" + dbusName,
930 "xyz.openbmc_project.Configuration." + *type,
James Feist02d2b932020-02-06 16:28:48 -0800931 board, true);
James Feist68500ff2018-08-08 15:40:42 -0700932 // permission is read-write, as since we just created it, must be
933 // runtime modifiable
934 populateInterfaceFromJson(
935 systemConfiguration,
James Feist16a02f22019-05-13 15:21:37 -0700936 jsonPointerPath + "/Exposes/" + std::to_string(lastIndex),
James Feist98132792019-07-09 13:29:09 -0700937 interface, newData, objServer,
James Feist68500ff2018-08-08 15:40:42 -0700938 sdbusplus::asio::PropertyPermission::readWrite);
James Feist68500ff2018-08-08 15:40:42 -0700939 });
940 iface->initialize();
941}
942
James Feista465ccc2019-02-08 12:51:01 -0800943void postToDbus(const nlohmann::json& newConfiguration,
944 nlohmann::json& systemConfiguration,
945 sdbusplus::asio::object_server& objServer)
James Feist75fdeeb2018-02-20 14:26:16 -0800946
James Feist1b2e2242018-01-30 13:45:19 -0800947{
James Feist97a63f12018-05-17 13:50:57 -0700948 // iterate through boards
James Feista465ccc2019-02-08 12:51:01 -0800949 for (auto& boardPair : newConfiguration.items())
James Feist1b2e2242018-01-30 13:45:19 -0800950 {
James Feistf1b14142019-04-10 15:22:09 -0700951 std::string boardKey = boardPair.value()["Name"];
James Feistd58879a2019-09-11 11:26:07 -0700952 std::string boardKeyOrig = boardPair.value()["Name"];
James Feistf1b14142019-04-10 15:22:09 -0700953 std::string jsonPointerPath = "/" + boardPair.key();
James Feist97a63f12018-05-17 13:50:57 -0700954 // loop through newConfiguration, but use values from system
955 // configuration to be able to modify via dbus later
James Feistf1b14142019-04-10 15:22:09 -0700956 auto boardValues = systemConfiguration[boardPair.key()];
James Feistd63d18a2018-07-19 15:23:45 -0700957 auto findBoardType = boardValues.find("Type");
James Feist1b2e2242018-01-30 13:45:19 -0800958 std::string boardType;
959 if (findBoardType != boardValues.end() &&
960 findBoardType->type() == nlohmann::json::value_t::string)
961 {
962 boardType = findBoardType->get<std::string>();
963 std::regex_replace(boardType.begin(), boardType.begin(),
Johnathan Mantey2015f752019-03-26 15:22:31 -0700964 boardType.end(), ILLEGAL_DBUS_MEMBER_REGEX, "_");
James Feist1b2e2242018-01-30 13:45:19 -0800965 }
966 else
967 {
968 std::cerr << "Unable to find type for " << boardKey
969 << " reverting to Chassis.\n";
970 boardType = "Chassis";
971 }
James Feist11be6672018-04-06 14:05:32 -0700972 std::string boardtypeLower = boost::algorithm::to_lower_copy(boardType);
James Feist1b2e2242018-01-30 13:45:19 -0800973
974 std::regex_replace(boardKey.begin(), boardKey.begin(), boardKey.end(),
Johnathan Mantey2015f752019-03-26 15:22:31 -0700975 ILLEGAL_DBUS_MEMBER_REGEX, "_");
James Feist11be6672018-04-06 14:05:32 -0700976 std::string boardName = "/xyz/openbmc_project/inventory/system/" +
977 boardtypeLower + "/" + boardKey;
James Feist1b2e2242018-01-30 13:45:19 -0800978
James Feistd58879a2019-09-11 11:26:07 -0700979 std::shared_ptr<sdbusplus::asio::dbus_interface> inventoryIface =
980 createInterface(objServer, boardName,
981 "xyz.openbmc_project.Inventory.Item", boardKey);
James Feist68500ff2018-08-08 15:40:42 -0700982
James Feistd58879a2019-09-11 11:26:07 -0700983 std::shared_ptr<sdbusplus::asio::dbus_interface> boardIface =
984 createInterface(objServer, boardName,
985 "xyz.openbmc_project.Inventory.Item." + boardType,
986 boardKeyOrig);
James Feist11be6672018-04-06 14:05:32 -0700987
James Feist68500ff2018-08-08 15:40:42 -0700988 createAddObjectMethod(jsonPointerPath, boardName, systemConfiguration,
James Feistd58879a2019-09-11 11:26:07 -0700989 objServer, boardKeyOrig);
James Feist68500ff2018-08-08 15:40:42 -0700990
James Feist97a63f12018-05-17 13:50:57 -0700991 populateInterfaceFromJson(systemConfiguration, jsonPointerPath,
James Feistc6248a52018-08-14 10:09:45 -0700992 boardIface, boardValues, objServer);
James Feist97a63f12018-05-17 13:50:57 -0700993 jsonPointerPath += "/";
994 // iterate through board properties
James Feista465ccc2019-02-08 12:51:01 -0800995 for (auto& boardField : boardValues.items())
James Feist11be6672018-04-06 14:05:32 -0700996 {
997 if (boardField.value().type() == nlohmann::json::value_t::object)
998 {
James Feistd58879a2019-09-11 11:26:07 -0700999 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
1000 createInterface(objServer, boardName, boardField.key(),
1001 boardKeyOrig);
1002
James Feistc6248a52018-08-14 10:09:45 -07001003 populateInterfaceFromJson(systemConfiguration,
1004 jsonPointerPath + boardField.key(),
1005 iface, boardField.value(), objServer);
James Feist11be6672018-04-06 14:05:32 -07001006 }
1007 }
James Feist97a63f12018-05-17 13:50:57 -07001008
James Feist1e3e6982018-08-03 16:09:28 -07001009 auto exposes = boardValues.find("Exposes");
James Feist1b2e2242018-01-30 13:45:19 -08001010 if (exposes == boardValues.end())
1011 {
1012 continue;
1013 }
James Feist97a63f12018-05-17 13:50:57 -07001014 // iterate through exposes
James Feist1e3e6982018-08-03 16:09:28 -07001015 jsonPointerPath += "Exposes/";
James Feist97a63f12018-05-17 13:50:57 -07001016
1017 // store the board level pointer so we can modify it on the way down
1018 std::string jsonPointerPathBoard = jsonPointerPath;
1019 size_t exposesIndex = -1;
James Feista465ccc2019-02-08 12:51:01 -08001020 for (auto& item : *exposes)
James Feist1b2e2242018-01-30 13:45:19 -08001021 {
James Feist97a63f12018-05-17 13:50:57 -07001022 exposesIndex++;
1023 jsonPointerPath = jsonPointerPathBoard;
1024 jsonPointerPath += std::to_string(exposesIndex);
1025
James Feistd63d18a2018-07-19 15:23:45 -07001026 auto findName = item.find("Name");
James Feist1b2e2242018-01-30 13:45:19 -08001027 if (findName == item.end())
1028 {
1029 std::cerr << "cannot find name in field " << item << "\n";
1030 continue;
1031 }
James Feist1e3e6982018-08-03 16:09:28 -07001032 auto findStatus = item.find("Status");
James Feist1b2e2242018-01-30 13:45:19 -08001033 // if status is not found it is assumed to be status = 'okay'
1034 if (findStatus != item.end())
1035 {
1036 if (*findStatus == "disabled")
1037 {
1038 continue;
1039 }
1040 }
James Feistd63d18a2018-07-19 15:23:45 -07001041 auto findType = item.find("Type");
James Feist1b2e2242018-01-30 13:45:19 -08001042 std::string itemType;
1043 if (findType != item.end())
1044 {
1045 itemType = findType->get<std::string>();
1046 std::regex_replace(itemType.begin(), itemType.begin(),
Johnathan Mantey2015f752019-03-26 15:22:31 -07001047 itemType.end(), ILLEGAL_DBUS_PATH_REGEX,
1048 "_");
James Feist1b2e2242018-01-30 13:45:19 -08001049 }
1050 else
1051 {
1052 itemType = "unknown";
1053 }
1054 std::string itemName = findName->get<std::string>();
1055 std::regex_replace(itemName.begin(), itemName.begin(),
Johnathan Mantey2015f752019-03-26 15:22:31 -07001056 itemName.end(), ILLEGAL_DBUS_MEMBER_REGEX, "_");
James Feistc6248a52018-08-14 10:09:45 -07001057
James Feistd58879a2019-09-11 11:26:07 -07001058 std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface =
1059 createInterface(objServer, boardName + "/" + itemName,
1060 "xyz.openbmc_project.Configuration." + itemType,
1061 boardKeyOrig);
James Feist1b2e2242018-01-30 13:45:19 -08001062
James Feist97a63f12018-05-17 13:50:57 -07001063 populateInterfaceFromJson(systemConfiguration, jsonPointerPath,
James Feistc6248a52018-08-14 10:09:45 -07001064 itemIface, item, objServer,
1065 getPermission(itemType));
James Feist1b2e2242018-01-30 13:45:19 -08001066
James Feista465ccc2019-02-08 12:51:01 -08001067 for (auto& objectPair : item.items())
James Feist1b2e2242018-01-30 13:45:19 -08001068 {
James Feist97a63f12018-05-17 13:50:57 -07001069 jsonPointerPath = jsonPointerPathBoard +
1070 std::to_string(exposesIndex) + "/" +
1071 objectPair.key();
James Feist1b2e2242018-01-30 13:45:19 -08001072 if (objectPair.value().type() ==
1073 nlohmann::json::value_t::object)
1074 {
James Feistd58879a2019-09-11 11:26:07 -07001075 std::shared_ptr<sdbusplus::asio::dbus_interface>
1076 objectIface = createInterface(
1077 objServer, boardName + "/" + itemName,
1078 "xyz.openbmc_project.Configuration." + itemType +
1079 "." + objectPair.key(),
1080 boardKeyOrig);
James Feist97a63f12018-05-17 13:50:57 -07001081
Adrian Ambrożewiczc789fca2020-05-14 15:50:05 +02001082 populateInterfaceFromJson(systemConfiguration,
1083 jsonPointerPath, objectIface,
1084 objectPair.value(), objServer,
1085 getPermission(objectPair.key()));
James Feist1b2e2242018-01-30 13:45:19 -08001086 }
1087 else if (objectPair.value().type() ==
1088 nlohmann::json::value_t::array)
1089 {
1090 size_t index = 0;
James Feist8f2710a2018-05-09 17:18:55 -07001091 if (!objectPair.value().size())
James Feist1b2e2242018-01-30 13:45:19 -08001092 {
James Feist8f2710a2018-05-09 17:18:55 -07001093 continue;
1094 }
1095 bool isLegal = true;
1096 auto type = objectPair.value()[0].type();
1097 if (type != nlohmann::json::value_t::object)
1098 {
1099 continue;
1100 }
1101
1102 // verify legal json
James Feista465ccc2019-02-08 12:51:01 -08001103 for (const auto& arrayItem : objectPair.value())
James Feist8f2710a2018-05-09 17:18:55 -07001104 {
1105 if (arrayItem.type() != type)
James Feist1b2e2242018-01-30 13:45:19 -08001106 {
James Feist8f2710a2018-05-09 17:18:55 -07001107 isLegal = false;
James Feist1b2e2242018-01-30 13:45:19 -08001108 break;
1109 }
James Feist8f2710a2018-05-09 17:18:55 -07001110 }
1111 if (!isLegal)
1112 {
1113 std::cerr << "dbus format error" << objectPair.value()
1114 << "\n";
1115 break;
1116 }
1117
James Feista465ccc2019-02-08 12:51:01 -08001118 for (auto& arrayItem : objectPair.value())
James Feist8f2710a2018-05-09 17:18:55 -07001119 {
James Feist97a63f12018-05-17 13:50:57 -07001120
James Feistd58879a2019-09-11 11:26:07 -07001121 std::shared_ptr<sdbusplus::asio::dbus_interface>
1122 objectIface = createInterface(
1123 objServer, boardName + "/" + itemName,
1124 "xyz.openbmc_project.Configuration." +
1125 itemType + "." + objectPair.key() +
1126 std::to_string(index),
1127 boardKeyOrig);
1128
James Feistc6248a52018-08-14 10:09:45 -07001129 populateInterfaceFromJson(
1130 systemConfiguration,
1131 jsonPointerPath + "/" + std::to_string(index),
1132 objectIface, arrayItem, objServer,
1133 getPermission(objectPair.key()));
James Feistbb43d022018-06-12 15:44:33 -07001134 index++;
James Feist1b2e2242018-01-30 13:45:19 -08001135 }
1136 }
1137 }
1138 }
1139 }
1140}
1141
James Feist8f2710a2018-05-09 17:18:55 -07001142// reads json files out of the filesystem
James Feista465ccc2019-02-08 12:51:01 -08001143bool findJsonFiles(std::list<nlohmann::json>& configurations)
James Feist3cb5fec2018-01-23 14:41:51 -08001144{
1145 // find configuration files
Ed Tanous072e25d2018-12-16 21:45:20 -08001146 std::vector<std::filesystem::path> jsonPaths;
Johnathan Mantey2015f752019-03-26 15:22:31 -07001147 if (!findFiles(std::filesystem::path(configurationDirectory), R"(.*\.json)",
1148 jsonPaths))
James Feist3cb5fec2018-01-23 14:41:51 -08001149 {
1150 std::cerr << "Unable to find any configuration files in "
James Feistb4383f42018-08-06 16:54:10 -07001151 << configurationDirectory << "\n";
James Feist75fdeeb2018-02-20 14:26:16 -08001152 return false;
James Feist3cb5fec2018-01-23 14:41:51 -08001153 }
James Feistb4383f42018-08-06 16:54:10 -07001154
1155 std::ifstream schemaStream(std::string(schemaDirectory) + "/" +
1156 globalSchema);
1157 if (!schemaStream.good())
1158 {
1159 std::cerr
1160 << "Cannot open schema file, cannot validate JSON, exiting\n\n";
1161 std::exit(EXIT_FAILURE);
Ed Tanous072e25d2018-12-16 21:45:20 -08001162 return false;
James Feistb4383f42018-08-06 16:54:10 -07001163 }
1164 nlohmann::json schema = nlohmann::json::parse(schemaStream, nullptr, false);
1165 if (schema.is_discarded())
1166 {
1167 std::cerr
1168 << "Illegal schema file detected, cannot validate JSON, exiting\n";
1169 std::exit(EXIT_FAILURE);
Ed Tanous072e25d2018-12-16 21:45:20 -08001170 return false;
James Feistb4383f42018-08-06 16:54:10 -07001171 }
1172
James Feista465ccc2019-02-08 12:51:01 -08001173 for (auto& jsonPath : jsonPaths)
James Feist3cb5fec2018-01-23 14:41:51 -08001174 {
1175 std::ifstream jsonStream(jsonPath.c_str());
1176 if (!jsonStream.good())
1177 {
1178 std::cerr << "unable to open " << jsonPath.string() << "\n";
1179 continue;
1180 }
1181 auto data = nlohmann::json::parse(jsonStream, nullptr, false);
1182 if (data.is_discarded())
1183 {
1184 std::cerr << "syntax error in " << jsonPath.string() << "\n";
1185 continue;
1186 }
James Feist8da99192019-01-24 08:20:16 -08001187 /*
1188 * todo(james): reenable this once less things are in flight
1189 *
James Feistb4383f42018-08-06 16:54:10 -07001190 if (!validateJson(schema, data))
1191 {
1192 std::cerr << "Error validating " << jsonPath.string() << "\n";
1193 continue;
1194 }
James Feist8da99192019-01-24 08:20:16 -08001195 */
James Feistb4383f42018-08-06 16:54:10 -07001196
James Feist3cb5fec2018-01-23 14:41:51 -08001197 if (data.type() == nlohmann::json::value_t::array)
1198 {
James Feista465ccc2019-02-08 12:51:01 -08001199 for (auto& d : data)
James Feist3cb5fec2018-01-23 14:41:51 -08001200 {
1201 configurations.emplace_back(d);
1202 }
1203 }
1204 else
1205 {
1206 configurations.emplace_back(data);
1207 }
1208 }
Ed Tanous072e25d2018-12-16 21:45:20 -08001209 return true;
James Feist75fdeeb2018-02-20 14:26:16 -08001210}
James Feist3cb5fec2018-01-23 14:41:51 -08001211
James Feist94219d12020-03-03 11:52:25 -08001212std::string getRecordName(
1213 const boost::container::flat_map<std::string, BasicVariantType>& probe,
1214 const std::string& probeName)
1215{
1216 if (probe.empty())
1217 {
1218 return probeName;
1219 }
1220
1221 // use an array so alphabetical order from the
1222 // flat_map is maintained
1223 auto device = nlohmann::json::array();
1224 for (auto& devPair : probe)
1225 {
1226 device.push_back(devPair.first);
1227 std::visit([&device](auto&& v) { device.push_back(v); },
1228 devPair.second);
1229 }
1230 size_t hash = std::hash<std::string>{}(probeName + device.dump());
1231 // hashes are hard to distinguish, use the
1232 // non-hashed version if we want debug
1233 if constexpr (DEBUG)
1234 {
1235 return probeName + device.dump();
1236 }
1237 else
1238 {
1239 return std::to_string(hash);
1240 }
1241}
1242
James Feist4dc617b2020-05-01 09:54:47 -07001243PerformScan::PerformScan(nlohmann::json& systemConfiguration,
1244 nlohmann::json& missingConfigurations,
1245 std::list<nlohmann::json>& configurations,
1246 sdbusplus::asio::object_server& objServerIn,
1247 std::function<void()>&& callback) :
James Feist733f7652019-11-13 14:32:29 -08001248 _systemConfiguration(systemConfiguration),
1249 _missingConfigurations(missingConfigurations),
James Feist4dc617b2020-05-01 09:54:47 -07001250 _configurations(configurations), objServer(objServerIn),
1251 _callback(std::move(callback))
James Feist8c505da2020-05-28 10:06:33 -07001252{}
James Feist733f7652019-11-13 14:32:29 -08001253void PerformScan::run()
1254{
1255 boost::container::flat_set<std::string> dbusProbeInterfaces;
1256 std::vector<std::shared_ptr<PerformProbe>> dbusProbePointers;
James Feist75fdeeb2018-02-20 14:26:16 -08001257
James Feist733f7652019-11-13 14:32:29 -08001258 for (auto it = _configurations.begin(); it != _configurations.end();)
James Feist3cb5fec2018-01-23 14:41:51 -08001259 {
James Feist733f7652019-11-13 14:32:29 -08001260 auto findProbe = it->find("Probe");
1261 auto findName = it->find("Name");
James Feist787c3c32019-11-07 14:42:58 -08001262
James Feist733f7652019-11-13 14:32:29 -08001263 nlohmann::json probeCommand;
1264 // check for poorly formatted fields, probe must be an array
1265 if (findProbe == it->end())
James Feist3cb5fec2018-01-23 14:41:51 -08001266 {
James Feist733f7652019-11-13 14:32:29 -08001267 std::cerr << "configuration file missing probe:\n " << *it << "\n";
1268 it = _configurations.erase(it);
1269 continue;
1270 }
1271 else if ((*findProbe).type() != nlohmann::json::value_t::array)
1272 {
1273 probeCommand = nlohmann::json::array();
1274 probeCommand.push_back(*findProbe);
1275 }
1276 else
1277 {
1278 probeCommand = *findProbe;
1279 }
James Feist3cb5fec2018-01-23 14:41:51 -08001280
James Feist733f7652019-11-13 14:32:29 -08001281 if (findName == it->end())
1282 {
1283 std::cerr << "configuration file missing name:\n " << *it << "\n";
1284 it = _configurations.erase(it);
1285 continue;
1286 }
1287 std::string probeName = *findName;
James Feist1b2e2242018-01-30 13:45:19 -08001288
James Feist733f7652019-11-13 14:32:29 -08001289 if (std::find(passedProbes.begin(), passedProbes.end(), probeName) !=
1290 passedProbes.end())
1291 {
1292 it = _configurations.erase(it);
1293 continue;
1294 }
1295 nlohmann::json* recordPtr = &(*it);
James Feist1b2e2242018-01-30 13:45:19 -08001296
James Feist733f7652019-11-13 14:32:29 -08001297 // store reference to this to children to makes sure we don't get
1298 // destroyed too early
1299 auto thisRef = shared_from_this();
1300 auto probePointer = std::make_shared<PerformProbe>(
1301 probeCommand, thisRef,
Matt Spinlere789bf12021-02-24 12:33:01 -06001302 [&, recordPtr, probeName](FoundDeviceT& foundDevices,
1303 const DBusProbeObjectT& allInterfaces) {
James Feist08a5b172019-08-28 14:47:47 -07001304 _passed = true;
James Feist35f5e0e2020-03-16 14:02:27 -07001305 std::set<nlohmann::json> usedNames;
James Feist733f7652019-11-13 14:32:29 -08001306 passedProbes.push_back(probeName);
James Feist94219d12020-03-03 11:52:25 -08001307 std::list<size_t> indexes(foundDevices.size());
1308 std::iota(indexes.begin(), indexes.end(), 1);
James Feist8f2710a2018-05-09 17:18:55 -07001309
James Feist35f5e0e2020-03-16 14:02:27 -07001310 size_t indexIdx = probeName.find("$");
1311 bool hasTemplateName = (indexIdx != std::string::npos);
James Feist94219d12020-03-03 11:52:25 -08001312
1313 // copy over persisted configurations and make sure we remove
1314 // indexes that are already used
1315 for (auto itr = foundDevices.begin();
1316 itr != foundDevices.end();)
James Feist08a5b172019-08-28 14:47:47 -07001317 {
Matt Spinlere789bf12021-02-24 12:33:01 -06001318 std::string recordName =
1319 getRecordName(std::get<0>(*itr), probeName);
James Feistf1b14142019-04-10 15:22:09 -07001320
James Feist08a5b172019-08-28 14:47:47 -07001321 auto fromLastJson = lastJson.find(recordName);
1322 if (fromLastJson != lastJson.end())
1323 {
James Feist02d2b932020-02-06 16:28:48 -08001324 auto findExposes = fromLastJson->find("Exposes");
1325 // delete nulls from any updates
1326 if (findExposes != fromLastJson->end())
1327 {
1328 auto copy = nlohmann::json::array();
1329 for (auto& expose : *findExposes)
1330 {
1331 if (expose.is_null())
1332 {
1333 continue;
1334 }
1335 copy.emplace_back(expose);
1336 }
1337 *findExposes = copy;
1338 }
1339
James Feist08a5b172019-08-28 14:47:47 -07001340 // keep user changes
1341 _systemConfiguration[recordName] = *fromLastJson;
James Feist899e17f2019-09-13 11:46:29 -07001342 _missingConfigurations.erase(recordName);
James Feist94219d12020-03-03 11:52:25 -08001343 itr = foundDevices.erase(itr);
James Feist35f5e0e2020-03-16 14:02:27 -07001344 if (hasTemplateName)
James Feist94219d12020-03-03 11:52:25 -08001345 {
1346 auto nameIt = fromLastJson->find("Name");
1347 if (nameIt == fromLastJson->end())
1348 {
1349 std::cerr << "Last JSON Illegal\n";
1350 continue;
1351 }
Igor Kononenko9fd87e52020-10-06 01:18:17 +03001352 int index = 0;
1353 auto str =
1354 nameIt->get<std::string>().substr(indexIdx);
1355 auto [p, ec] = std::from_chars(
1356 str.data(), str.data() + str.size(), index);
1357 if (ec != std::errc())
1358 {
1359 continue; // non-numeric replacement
1360 }
James Feist35f5e0e2020-03-16 14:02:27 -07001361 usedNames.insert(nameIt.value());
James Feist94219d12020-03-03 11:52:25 -08001362 auto usedIt = std::find(indexes.begin(),
1363 indexes.end(), index);
1364
1365 if (usedIt == indexes.end())
1366 {
1367 continue; // less items now
1368 }
1369 indexes.erase(usedIt);
1370 }
1371
James Feist08a5b172019-08-28 14:47:47 -07001372 continue;
1373 }
James Feist94219d12020-03-03 11:52:25 -08001374 itr++;
1375 }
James Feist35f5e0e2020-03-16 14:02:27 -07001376
1377 std::optional<std::string> replaceStr;
1378
Matt Spinler4bab9322021-04-08 14:38:43 -05001379 DBusProbeObjectT::mapped_type emptyInterfaces;
1380 boost::container::flat_map<std::string, BasicVariantType>
1381 emptyProps;
1382 emptyInterfaces.emplace(std::string{}, emptyProps);
1383
Matt Spinlere789bf12021-02-24 12:33:01 -06001384 for (auto& foundDeviceAndPath : foundDevices)
James Feist94219d12020-03-03 11:52:25 -08001385 {
Matt Spinlere789bf12021-02-24 12:33:01 -06001386 const boost::container::flat_map<
1387 std::string, BasicVariantType>& foundDevice =
1388 std::get<0>(foundDeviceAndPath);
1389 const std::string& path = std::get<1>(foundDeviceAndPath);
1390
1391 // Need all interfaces on this path so that template
1392 // substitutions can be done with any of the contained
Matt Spinler4bab9322021-04-08 14:38:43 -05001393 // properties. If the probe that passed didn't use an
1394 // interface, such as if it was just TRUE, then
1395 // templateCharReplace will just get passed in an empty
1396 // map.
1397 const DBusProbeObjectT::mapped_type* allInterfacesOnPath =
1398 &emptyInterfaces;
1399
1400 auto ifacesIt = allInterfaces.find(path);
1401 if (ifacesIt != allInterfaces.end())
Matt Spinlere789bf12021-02-24 12:33:01 -06001402 {
Matt Spinler4bab9322021-04-08 14:38:43 -05001403 allInterfacesOnPath = &ifacesIt->second;
Matt Spinlere789bf12021-02-24 12:33:01 -06001404 }
1405
James Feist94219d12020-03-03 11:52:25 -08001406 nlohmann::json record = *recordPtr;
1407 std::string recordName =
1408 getRecordName(foundDevice, probeName);
1409 size_t foundDeviceIdx = indexes.front();
1410 indexes.pop_front();
James Feistf1b14142019-04-10 15:22:09 -07001411
James Feist35f5e0e2020-03-16 14:02:27 -07001412 // check name first so we have no duplicate names
1413 auto getName = record.find("Name");
1414 if (getName == record.end())
1415 {
1416 std::cerr << "Record Missing Name! " << record.dump();
1417 continue; // this should be impossible at this level
1418 }
1419
1420 nlohmann::json copyForName = {{"Name", getName.value()}};
1421 nlohmann::json::iterator copyIt = copyForName.begin();
Matt Spinlere789bf12021-02-24 12:33:01 -06001422 std::optional<std::string> replaceVal =
Matt Spinler4bab9322021-04-08 14:38:43 -05001423 templateCharReplace(copyIt, *allInterfacesOnPath,
Matt Spinlere789bf12021-02-24 12:33:01 -06001424 foundDeviceIdx, replaceStr);
James Feist35f5e0e2020-03-16 14:02:27 -07001425
1426 if (!replaceStr && replaceVal)
1427 {
1428 if (usedNames.find(copyIt.value()) != usedNames.end())
1429 {
1430 replaceStr = replaceVal;
1431 copyForName = {{"Name", getName.value()}};
1432 copyIt = copyForName.begin();
Matt Spinler4bab9322021-04-08 14:38:43 -05001433 templateCharReplace(copyIt, *allInterfacesOnPath,
James Feist35f5e0e2020-03-16 14:02:27 -07001434 foundDeviceIdx, replaceStr);
1435 }
1436 }
1437
1438 if (replaceStr)
1439 {
1440 std::cerr << "Duplicates found, replacing "
1441 << *replaceStr
1442 << " with found device index.\n Consider "
1443 "fixing template to not have duplicates\n";
1444 }
James Feist08a5b172019-08-28 14:47:47 -07001445
1446 for (auto keyPair = record.begin(); keyPair != record.end();
1447 keyPair++)
1448 {
James Feist35f5e0e2020-03-16 14:02:27 -07001449 if (keyPair.key() == "Name")
1450 {
1451 keyPair.value() = copyIt.value();
1452 usedNames.insert(copyIt.value());
1453
1454 continue; // already covered above
1455 }
Matt Spinler4bab9322021-04-08 14:38:43 -05001456 templateCharReplace(keyPair, *allInterfacesOnPath,
James Feist35f5e0e2020-03-16 14:02:27 -07001457 foundDeviceIdx, replaceStr);
James Feist08a5b172019-08-28 14:47:47 -07001458 }
1459
James Feist35f5e0e2020-03-16 14:02:27 -07001460 // insert into configuration temporarily to be able to
1461 // reference ourselves
1462
1463 _systemConfiguration[recordName] = record;
1464
James Feist08a5b172019-08-28 14:47:47 -07001465 auto findExpose = record.find("Exposes");
1466 if (findExpose == record.end())
1467 {
James Feistf1b14142019-04-10 15:22:09 -07001468 _systemConfiguration[recordName] = record;
James Feist08a5b172019-08-28 14:47:47 -07001469 continue;
1470 }
James Feistf1b14142019-04-10 15:22:09 -07001471
James Feist08a5b172019-08-28 14:47:47 -07001472 for (auto& expose : *findExpose)
1473 {
1474 for (auto keyPair = expose.begin();
1475 keyPair != expose.end(); keyPair++)
James Feistf1b14142019-04-10 15:22:09 -07001476 {
James Feist08a5b172019-08-28 14:47:47 -07001477
Matt Spinler4bab9322021-04-08 14:38:43 -05001478 templateCharReplace(keyPair, *allInterfacesOnPath,
James Feist35f5e0e2020-03-16 14:02:27 -07001479 foundDeviceIdx, replaceStr);
James Feist08a5b172019-08-28 14:47:47 -07001480
James Feist668bbb12020-02-05 14:27:26 -08001481 bool isBind =
1482 boost::starts_with(keyPair.key(), "Bind");
1483 bool isDisable = keyPair.key() == "DisableNode";
1484
1485 // special cases
1486 if (!(isBind || isDisable))
James Feistf1b14142019-04-10 15:22:09 -07001487 {
James Feist668bbb12020-02-05 14:27:26 -08001488 continue;
1489 }
1490
1491 if (keyPair.value().type() !=
1492 nlohmann::json::value_t::string &&
1493 keyPair.value().type() !=
1494 nlohmann::json::value_t::array)
1495 {
1496 std::cerr << "Value is invalid type "
1497 << keyPair.key() << "\n";
1498 continue;
1499 }
1500
1501 std::vector<std::string> matches;
1502 if (keyPair.value().type() ==
1503 nlohmann::json::value_t::string)
1504 {
1505 matches.emplace_back(keyPair.value());
1506 }
1507 else
1508 {
1509 for (const auto& value : keyPair.value())
James Feistf1b14142019-04-10 15:22:09 -07001510 {
James Feist668bbb12020-02-05 14:27:26 -08001511 if (value.type() !=
1512 nlohmann::json::value_t::string)
1513 {
1514 std::cerr << "Value is invalid type "
1515 << value << "\n";
1516 break;
1517 }
1518 matches.emplace_back(value);
James Feistf1b14142019-04-10 15:22:09 -07001519 }
James Feist668bbb12020-02-05 14:27:26 -08001520 }
James Feist08a5b172019-08-28 14:47:47 -07001521
James Feist668bbb12020-02-05 14:27:26 -08001522 std::set<std::string> foundMatches;
1523 for (auto& configurationPair :
1524 _systemConfiguration.items())
1525 {
1526 if (isDisable)
James Feist8f2710a2018-05-09 17:18:55 -07001527 {
James Feist668bbb12020-02-05 14:27:26 -08001528 // don't disable ourselves
1529 if (configurationPair.key() == recordName)
James Feist3cb5fec2018-01-23 14:41:51 -08001530 {
James Feist1b2e2242018-01-30 13:45:19 -08001531 continue;
1532 }
James Feist668bbb12020-02-05 14:27:26 -08001533 }
1534 auto configListFind =
1535 configurationPair.value().find("Exposes");
James Feist8f2710a2018-05-09 17:18:55 -07001536
James Feist668bbb12020-02-05 14:27:26 -08001537 if (configListFind ==
1538 configurationPair.value().end() ||
1539 configListFind->type() !=
1540 nlohmann::json::value_t::array)
James Feist08a5b172019-08-28 14:47:47 -07001541 {
James Feist668bbb12020-02-05 14:27:26 -08001542 continue;
James Feist08a5b172019-08-28 14:47:47 -07001543 }
James Feist668bbb12020-02-05 14:27:26 -08001544 for (auto& exposedObject : *configListFind)
1545 {
1546 auto matchIt = std::find_if(
1547 matches.begin(), matches.end(),
1548 [name = (exposedObject)["Name"]
1549 .get<std::string>()](
1550 const std::string& s) {
1551 return s == name;
1552 });
1553 if (matchIt == matches.end())
1554 {
1555 continue;
1556 }
1557 foundMatches.insert(*matchIt);
1558
1559 if (isBind)
1560 {
1561 std::string bind = keyPair.key().substr(
1562 sizeof("Bind") - 1);
1563
1564 exposedObject["Status"] = "okay";
1565 expose[bind] = exposedObject;
1566 }
1567 else if (isDisable)
1568 {
1569 exposedObject["Status"] = "disabled";
1570 }
1571 }
1572 }
1573 if (foundMatches.size() != matches.size())
1574 {
1575 std::cerr << "configuration file "
1576 "dependency error, "
1577 "could not find "
1578 << keyPair.key() << " "
1579 << keyPair.value() << "\n";
James Feist3cb5fec2018-01-23 14:41:51 -08001580 }
1581 }
1582 }
James Feist08a5b172019-08-28 14:47:47 -07001583 // overwrite ourselves with cleaned up version
1584 _systemConfiguration[recordName] = record;
James Feist899e17f2019-09-13 11:46:29 -07001585 _missingConfigurations.erase(recordName);
James Feist08a5b172019-08-28 14:47:47 -07001586 }
1587 });
James Feist787c3c32019-11-07 14:42:58 -08001588
James Feist733f7652019-11-13 14:32:29 -08001589 // parse out dbus probes by discarding other probe types, store in a
1590 // map
Ed Tanouscda14732021-05-11 16:02:46 -07001591 for (const std::string probe : probeCommand)
James Feist733f7652019-11-13 14:32:29 -08001592 {
1593 bool found = false;
1594 boost::container::flat_map<const char*, probe_type_codes,
1595 cmp_str>::const_iterator probeType;
1596 for (probeType = PROBE_TYPES.begin();
1597 probeType != PROBE_TYPES.end(); ++probeType)
James Feist787c3c32019-11-07 14:42:58 -08001598 {
James Feist733f7652019-11-13 14:32:29 -08001599 if (probe.find(probeType->first) != std::string::npos)
James Feist787c3c32019-11-07 14:42:58 -08001600 {
James Feist733f7652019-11-13 14:32:29 -08001601 found = true;
1602 break;
James Feist787c3c32019-11-07 14:42:58 -08001603 }
James Feist787c3c32019-11-07 14:42:58 -08001604 }
James Feist733f7652019-11-13 14:32:29 -08001605 if (found)
1606 {
1607 continue;
1608 }
1609 // syntax requires probe before first open brace
1610 auto findStart = probe.find("(");
1611 std::string interface = probe.substr(0, findStart);
1612 dbusProbeInterfaces.emplace(interface);
1613 dbusProbePointers.emplace_back(probePointer);
James Feist3cb5fec2018-01-23 14:41:51 -08001614 }
James Feist733f7652019-11-13 14:32:29 -08001615 it++;
James Feist3cb5fec2018-01-23 14:41:51 -08001616 }
James Feist75fdeeb2018-02-20 14:26:16 -08001617
James Feist733f7652019-11-13 14:32:29 -08001618 // probe vector stores a shared_ptr to each PerformProbe that cares
1619 // about a dbus interface
James Feistb1728ca2020-04-30 15:40:55 -07001620 findDbusObjects(std::move(dbusProbePointers),
1621 std::move(dbusProbeInterfaces), shared_from_this());
1622 if constexpr (DEBUG)
1623 {
1624 std::cerr << __func__ << " " << __LINE__ << "\n";
1625 }
James Feist733f7652019-11-13 14:32:29 -08001626}
1627
1628PerformScan::~PerformScan()
1629{
1630 if (_passed)
James Feist75fdeeb2018-02-20 14:26:16 -08001631 {
James Feist733f7652019-11-13 14:32:29 -08001632 auto nextScan = std::make_shared<PerformScan>(
1633 _systemConfiguration, _missingConfigurations, _configurations,
James Feist4dc617b2020-05-01 09:54:47 -07001634 objServer, std::move(_callback));
James Feist733f7652019-11-13 14:32:29 -08001635 nextScan->passedProbes = std::move(passedProbes);
1636 nextScan->dbusProbeObjects = std::move(dbusProbeObjects);
1637 nextScan->run();
James Feistb1728ca2020-04-30 15:40:55 -07001638
1639 if constexpr (DEBUG)
1640 {
1641 std::cerr << __func__ << " " << __LINE__ << "\n";
1642 }
James Feist8f2710a2018-05-09 17:18:55 -07001643 }
James Feist733f7652019-11-13 14:32:29 -08001644 else
1645 {
James Feist4dc617b2020-05-01 09:54:47 -07001646 _callback();
James Feistb1728ca2020-04-30 15:40:55 -07001647
1648 if constexpr (DEBUG)
1649 {
1650 std::cerr << __func__ << " " << __LINE__ << "\n";
1651 }
James Feist733f7652019-11-13 14:32:29 -08001652 }
1653}
James Feistc95cb142018-02-26 10:41:42 -08001654
James Feistb1728ca2020-04-30 15:40:55 -07001655void startRemovedTimer(boost::asio::steady_timer& timer,
James Feist1df06a42019-04-11 14:23:04 -07001656 nlohmann::json& systemConfiguration)
1657{
1658 static bool scannedPowerOff = false;
1659 static bool scannedPowerOn = false;
1660
James Feistfb00f392019-06-25 14:16:48 -07001661 if (systemConfiguration.empty() || lastJson.empty())
1662 {
1663 return; // not ready yet
1664 }
James Feist1df06a42019-04-11 14:23:04 -07001665 if (scannedPowerOn)
1666 {
1667 return;
1668 }
1669
1670 if (!isPowerOn() && scannedPowerOff)
1671 {
1672 return;
1673 }
1674
James Feistb1728ca2020-04-30 15:40:55 -07001675 timer.expires_after(std::chrono::seconds(10));
James Feist1a996582019-05-14 15:10:06 -07001676 timer.async_wait(
1677 [&systemConfiguration](const boost::system::error_code& ec) {
1678 if (ec == boost::asio::error::operation_aborted)
James Feist1df06a42019-04-11 14:23:04 -07001679 {
James Feist1a996582019-05-14 15:10:06 -07001680 // we were cancelled
1681 return;
1682 }
1683
1684 bool powerOff = !isPowerOn();
1685 for (const auto& item : lastJson.items())
1686 {
1687 if (systemConfiguration.find(item.key()) ==
1688 systemConfiguration.end())
James Feist1df06a42019-04-11 14:23:04 -07001689 {
James Feist1a996582019-05-14 15:10:06 -07001690 bool isDetectedPowerOn = false;
1691 auto powerState = item.value().find("PowerState");
1692 if (powerState != item.value().end())
James Feist1df06a42019-04-11 14:23:04 -07001693 {
James Feist1a996582019-05-14 15:10:06 -07001694 auto ptr = powerState->get_ptr<const std::string*>();
1695 if (ptr)
James Feist1df06a42019-04-11 14:23:04 -07001696 {
James Feist1a996582019-05-14 15:10:06 -07001697 if (*ptr == "On" || *ptr == "BiosPost")
1698 {
1699 isDetectedPowerOn = true;
1700 }
James Feist1df06a42019-04-11 14:23:04 -07001701 }
1702 }
James Feist1a996582019-05-14 15:10:06 -07001703 if (powerOff && isDetectedPowerOn)
1704 {
1705 // power not on yet, don't know if it's there or not
1706 continue;
1707 }
1708 if (!powerOff && scannedPowerOff && isDetectedPowerOn)
1709 {
1710 // already logged it when power was off
1711 continue;
1712 }
James Feist1df06a42019-04-11 14:23:04 -07001713
James Feist1a996582019-05-14 15:10:06 -07001714 logDeviceRemoved(item.value());
1715 }
James Feist1df06a42019-04-11 14:23:04 -07001716 }
James Feist1a996582019-05-14 15:10:06 -07001717 scannedPowerOff = true;
1718 if (!powerOff)
1719 {
1720 scannedPowerOn = true;
1721 }
1722 });
James Feist1df06a42019-04-11 14:23:04 -07001723}
1724
James Feist8f2710a2018-05-09 17:18:55 -07001725// main properties changed entry
James Feist4dc617b2020-05-01 09:54:47 -07001726void propertiesChangedCallback(nlohmann::json& systemConfiguration,
1727 sdbusplus::asio::object_server& objServer)
James Feist8f2710a2018-05-09 17:18:55 -07001728{
James Feist2539ccd2020-05-01 16:15:08 -07001729 static bool inProgress = false;
James Feistb1728ca2020-04-30 15:40:55 -07001730 static boost::asio::steady_timer timer(io);
James Feist899e17f2019-09-13 11:46:29 -07001731 static size_t instance = 0;
1732 instance++;
1733 size_t count = instance;
James Feist1df06a42019-04-11 14:23:04 -07001734
James Feistb1728ca2020-04-30 15:40:55 -07001735 timer.expires_after(std::chrono::seconds(5));
James Feist8f2710a2018-05-09 17:18:55 -07001736
1737 // setup an async wait as we normally get flooded with new requests
James Feist4dc617b2020-05-01 09:54:47 -07001738 timer.async_wait([&systemConfiguration, &objServer,
James Feist899e17f2019-09-13 11:46:29 -07001739 count](const boost::system::error_code& ec) {
James Feist8f2710a2018-05-09 17:18:55 -07001740 if (ec == boost::asio::error::operation_aborted)
1741 {
1742 // we were cancelled
1743 return;
1744 }
1745 else if (ec)
1746 {
1747 std::cerr << "async wait error " << ec << "\n";
1748 return;
1749 }
1750
James Feist2539ccd2020-05-01 16:15:08 -07001751 if (inProgress)
1752 {
1753 propertiesChangedCallback(systemConfiguration, objServer);
1754 return;
1755 }
1756 inProgress = true;
1757
James Feist8f2710a2018-05-09 17:18:55 -07001758 nlohmann::json oldConfiguration = systemConfiguration;
James Feist899e17f2019-09-13 11:46:29 -07001759 auto missingConfigurations = std::make_shared<nlohmann::json>();
1760 *missingConfigurations = systemConfiguration;
1761
James Feist8f2710a2018-05-09 17:18:55 -07001762 std::list<nlohmann::json> configurations;
1763 if (!findJsonFiles(configurations))
1764 {
1765 std::cerr << "cannot find json files\n";
James Feist2539ccd2020-05-01 16:15:08 -07001766 inProgress = false;
James Feist8f2710a2018-05-09 17:18:55 -07001767 return;
1768 }
1769
1770 auto perfScan = std::make_shared<PerformScan>(
James Feist899e17f2019-09-13 11:46:29 -07001771 systemConfiguration, *missingConfigurations, configurations,
James Feist4dc617b2020-05-01 09:54:47 -07001772 objServer,
1773 [&systemConfiguration, &objServer, count, oldConfiguration,
1774 missingConfigurations]() {
James Feist899e17f2019-09-13 11:46:29 -07001775 // this is something that since ac has been applied to the bmc
1776 // we saw, and we no longer see it
1777 bool powerOff = !isPowerOn();
1778 for (const auto& item : missingConfigurations->items())
1779 {
1780 bool isDetectedPowerOn = false;
1781 auto powerState = item.value().find("PowerState");
1782 if (powerState != item.value().end())
1783 {
1784 auto ptr = powerState->get_ptr<const std::string*>();
1785 if (ptr)
1786 {
1787 if (*ptr == "On" || *ptr == "BiosPost")
1788 {
1789 isDetectedPowerOn = true;
1790 }
1791 }
1792 }
1793 if (powerOff && isDetectedPowerOn)
1794 {
1795 // power not on yet, don't know if it's there or not
1796 continue;
1797 }
1798 std::string name = item.value()["Name"].get<std::string>();
James Feist02d2b932020-02-06 16:28:48 -08001799 std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>&
James Feist899e17f2019-09-13 11:46:29 -07001800 ifaces = inventory[name];
1801 for (auto& iface : ifaces)
1802 {
James Feist02d2b932020-02-06 16:28:48 -08001803 auto sharedPtr = iface.lock();
1804 if (!sharedPtr)
1805 {
1806 continue; // was already deleted elsewhere
1807 }
1808 objServer.remove_interface(sharedPtr);
James Feist899e17f2019-09-13 11:46:29 -07001809 }
1810 ifaces.clear();
1811 systemConfiguration.erase(item.key());
1812 logDeviceRemoved(item.value());
1813 }
1814
James Feist8f2710a2018-05-09 17:18:55 -07001815 nlohmann::json newConfiguration = systemConfiguration;
James Feist4131aea2018-03-09 09:47:30 -08001816 for (auto it = newConfiguration.begin();
1817 it != newConfiguration.end();)
1818 {
1819 auto findKey = oldConfiguration.find(it.key());
1820 if (findKey != oldConfiguration.end())
1821 {
1822 it = newConfiguration.erase(it);
1823 }
1824 else
1825 {
1826 it++;
1827 }
1828 }
James Feist899e17f2019-09-13 11:46:29 -07001829 for (const auto& item : newConfiguration.items())
1830 {
1831 logDeviceAdded(item.value());
1832 }
1833
James Feist2539ccd2020-05-01 16:15:08 -07001834 inProgress = false;
1835
Jonathan Doman6d649822021-05-05 16:53:04 -07001836 io.post([count, newConfiguration, &systemConfiguration,
1837 &objServer]() {
James Feist8f2710a2018-05-09 17:18:55 -07001838 loadOverlays(newConfiguration);
James Feistce4367c2018-10-16 09:19:57 -07001839
Jonathan Doman6d649822021-05-05 16:53:04 -07001840 io.post([&systemConfiguration]() {
James Feistbb43d022018-06-12 15:44:33 -07001841 if (!writeJsonFiles(systemConfiguration))
1842 {
1843 std::cerr << "Error writing json files\n";
1844 }
1845 });
Jonathan Doman6d649822021-05-05 16:53:04 -07001846 io.post([count, newConfiguration, &systemConfiguration,
1847 &objServer]() {
James Feist97a63f12018-05-17 13:50:57 -07001848 postToDbus(newConfiguration, systemConfiguration,
1849 objServer);
James Feist899e17f2019-09-13 11:46:29 -07001850 if (count != instance)
James Feist1df06a42019-04-11 14:23:04 -07001851 {
James Feist899e17f2019-09-13 11:46:29 -07001852 return;
James Feist1df06a42019-04-11 14:23:04 -07001853 }
James Feist899e17f2019-09-13 11:46:29 -07001854 startRemovedTimer(timer, systemConfiguration);
James Feist8f2710a2018-05-09 17:18:55 -07001855 });
1856 });
1857 });
1858 perfScan->run();
1859 });
James Feist75fdeeb2018-02-20 14:26:16 -08001860}
1861
James Feist4dc617b2020-05-01 09:54:47 -07001862void registerCallback(nlohmann::json& systemConfiguration,
1863 sdbusplus::asio::object_server& objServer,
Matt Spinlere789bf12021-02-24 12:33:01 -06001864 const std::string& path)
James Feist75fdeeb2018-02-20 14:26:16 -08001865{
James Feist4dc617b2020-05-01 09:54:47 -07001866 static boost::container::flat_map<std::string, sdbusplus::bus::match::match>
1867 dbusMatches;
James Feist75fdeeb2018-02-20 14:26:16 -08001868
Matt Spinlere789bf12021-02-24 12:33:01 -06001869 auto find = dbusMatches.find(path);
James Feist4dc617b2020-05-01 09:54:47 -07001870 if (find != dbusMatches.end())
James Feist75fdeeb2018-02-20 14:26:16 -08001871 {
James Feist4dc617b2020-05-01 09:54:47 -07001872 return;
James Feist75fdeeb2018-02-20 14:26:16 -08001873 }
James Feist4dc617b2020-05-01 09:54:47 -07001874 std::function<void(sdbusplus::message::message & message)> eventHandler =
1875
1876 [&](sdbusplus::message::message&) {
1877 propertiesChangedCallback(systemConfiguration, objServer);
1878 };
1879
1880 sdbusplus::bus::match::match match(
1881 static_cast<sdbusplus::bus::bus&>(*SYSTEM_BUS),
Matt Spinlere789bf12021-02-24 12:33:01 -06001882 "type='signal',member='PropertiesChanged',path='" + path + "'",
James Feist4dc617b2020-05-01 09:54:47 -07001883 eventHandler);
Matt Spinlere789bf12021-02-24 12:33:01 -06001884 dbusMatches.emplace(path, std::move(match));
James Feist75fdeeb2018-02-20 14:26:16 -08001885}
1886
James Feist98132792019-07-09 13:29:09 -07001887int main()
James Feist75fdeeb2018-02-20 14:26:16 -08001888{
1889 // setup connection to dbus
James Feist8f2710a2018-05-09 17:18:55 -07001890 SYSTEM_BUS = std::make_shared<sdbusplus::asio::connection>(io);
James Feist75fdeeb2018-02-20 14:26:16 -08001891 SYSTEM_BUS->request_name("xyz.openbmc_project.EntityManager");
James Feist4131aea2018-03-09 09:47:30 -08001892
James Feist8f2710a2018-05-09 17:18:55 -07001893 sdbusplus::asio::object_server objServer(SYSTEM_BUS);
James Feistfd1264a2018-05-03 12:10:00 -07001894
James Feist8f2710a2018-05-09 17:18:55 -07001895 std::shared_ptr<sdbusplus::asio::dbus_interface> entityIface =
1896 objServer.add_interface("/xyz/openbmc_project/EntityManager",
1897 "xyz.openbmc_project.EntityManager");
James Feistfd1264a2018-05-03 12:10:00 -07001898
James Feist4131aea2018-03-09 09:47:30 -08001899 // to keep reference to the match / filter objects so they don't get
1900 // destroyed
James Feist8f2710a2018-05-09 17:18:55 -07001901
1902 nlohmann::json systemConfiguration = nlohmann::json::object();
1903
Brad Bishopc76af0f2020-12-04 13:50:23 -05001904 // We need a poke from DBus for static providers that create all their
1905 // objects prior to claiming a well-known name, and thus don't emit any
1906 // org.freedesktop.DBus.Properties signals. Similarly if a process exits
1907 // for any reason, expected or otherwise, we'll need a poke to remove
1908 // entities from DBus.
1909 sdbusplus::bus::match::match nameOwnerChangedMatch(
1910 static_cast<sdbusplus::bus::bus&>(*SYSTEM_BUS),
1911 sdbusplus::bus::match::rules::nameOwnerChanged(),
1912 [&](sdbusplus::message::message&) {
1913 propertiesChangedCallback(systemConfiguration, objServer);
1914 });
Brad Bishop10a8c5f2020-12-07 21:40:07 -05001915 // We also need a poke from DBus when new interfaces are created or
1916 // destroyed.
1917 sdbusplus::bus::match::match interfacesAddedMatch(
1918 static_cast<sdbusplus::bus::bus&>(*SYSTEM_BUS),
1919 sdbusplus::bus::match::rules::interfacesAdded(),
1920 [&](sdbusplus::message::message&) {
1921 propertiesChangedCallback(systemConfiguration, objServer);
1922 });
1923 sdbusplus::bus::match::match interfacesRemovedMatch(
1924 static_cast<sdbusplus::bus::bus&>(*SYSTEM_BUS),
1925 sdbusplus::bus::match::rules::interfacesRemoved(),
1926 [&](sdbusplus::message::message&) {
1927 propertiesChangedCallback(systemConfiguration, objServer);
1928 });
Brad Bishopc76af0f2020-12-04 13:50:23 -05001929
James Feist4dc617b2020-05-01 09:54:47 -07001930 io.post(
1931 [&]() { propertiesChangedCallback(systemConfiguration, objServer); });
James Feist4131aea2018-03-09 09:47:30 -08001932
James Feistfd1264a2018-05-03 12:10:00 -07001933 entityIface->register_method("ReScan", [&]() {
James Feist4dc617b2020-05-01 09:54:47 -07001934 propertiesChangedCallback(systemConfiguration, objServer);
James Feist75fdeeb2018-02-20 14:26:16 -08001935 });
James Feist8f2710a2018-05-09 17:18:55 -07001936 entityIface->initialize();
1937
James Feist1df06a42019-04-11 14:23:04 -07001938 if (fwVersionIsSame())
1939 {
1940 if (std::filesystem::is_regular_file(currentConfiguration))
1941 {
1942 // this file could just be deleted, but it's nice for debug
1943 std::filesystem::create_directory(tempConfigDir);
1944 std::filesystem::remove(lastConfiguration);
1945 std::filesystem::copy(currentConfiguration, lastConfiguration);
1946 std::filesystem::remove(currentConfiguration);
1947
1948 std::ifstream jsonStream(lastConfiguration);
1949 if (jsonStream.good())
1950 {
1951 auto data = nlohmann::json::parse(jsonStream, nullptr, false);
1952 if (data.is_discarded())
1953 {
1954 std::cerr << "syntax error in " << lastConfiguration
1955 << "\n";
1956 }
1957 else
1958 {
1959 lastJson = std::move(data);
1960 }
1961 }
1962 else
1963 {
1964 std::cerr << "unable to open " << lastConfiguration << "\n";
1965 }
1966 }
1967 }
1968 else
1969 {
1970 // not an error, just logging at this level to make it in the journal
1971 std::cerr << "Clearing previous configuration\n";
1972 std::filesystem::remove(currentConfiguration);
1973 }
1974
1975 // some boards only show up after power is on, we want to not say they are
1976 // removed until the same state happens
1977 setupPowerMatch(SYSTEM_BUS);
1978
James Feist1b2e2242018-01-30 13:45:19 -08001979 io.run();
James Feist3cb5fec2018-01-23 14:41:51 -08001980
1981 return 0;
James Feist75fdeeb2018-02-20 14:26:16 -08001982}