blob: 25cc86cab5972f0b9b93d7aa7d650ea114857482 [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*/
16
James Feist1df06a42019-04-11 14:23:04 -070017#include "EntityManager.hpp"
18
James Feist481c5d52019-08-13 14:40:40 -070019#include "VariantVisitors.hpp"
20
James Feistc95cb142018-02-26 10:41:42 -080021#include <Overlay.hpp>
James Feista465ccc2019-02-08 12:51:01 -080022#include <Utils.hpp>
23#include <VariantVisitors.hpp>
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 Feist3cb5fec2018-01-23 14:41:51 -080029#include <boost/container/flat_map.hpp>
30#include <boost/container/flat_set.hpp>
James Feistf5125b02019-06-06 11:27:43 -070031#include <boost/range/iterator_range.hpp>
James Feist637b3ef2019-04-15 16:35:30 -070032#include <filesystem>
James Feista465ccc2019-02-08 12:51:01 -080033#include <fstream>
34#include <iostream>
35#include <nlohmann/json.hpp>
36#include <regex>
37#include <sdbusplus/asio/connection.hpp>
38#include <sdbusplus/asio/object_server.hpp>
39#include <variant>
James Feist3cb5fec2018-01-23 14:41:51 -080040
James Feista465ccc2019-02-08 12:51:01 -080041constexpr const char* configurationDirectory = PACKAGE_DIR "configurations";
42constexpr const char* schemaDirectory = PACKAGE_DIR "configurations/schemas";
James Feist1df06a42019-04-11 14:23:04 -070043constexpr const char* tempConfigDir = "/tmp/configuration/";
44constexpr const char* lastConfiguration = "/tmp/configuration/last.json";
45constexpr const char* currentConfiguration = "/var/configuration/system.json";
James Feista465ccc2019-02-08 12:51:01 -080046constexpr const char* globalSchema = "global.json";
James Feist8f2710a2018-05-09 17:18:55 -070047constexpr const int32_t MAX_MAPPER_DEPTH = 0;
James Feist3cb5fec2018-01-23 14:41:51 -080048
James Feistf1b14142019-04-10 15:22:09 -070049constexpr const bool DEBUG = false;
50
James Feist3cb5fec2018-01-23 14:41:51 -080051struct cmp_str
52{
James Feista465ccc2019-02-08 12:51:01 -080053 bool operator()(const char* a, const char* b) const
James Feist3cb5fec2018-01-23 14:41:51 -080054 {
55 return std::strcmp(a, b) < 0;
56 }
57};
58
James Feist8f2710a2018-05-09 17:18:55 -070059struct PerformProbe;
60
James Feist3cb5fec2018-01-23 14:41:51 -080061// underscore T for collison with dbus c api
62enum class probe_type_codes
63{
64 FALSE_T,
65 TRUE_T,
66 AND,
67 OR,
James Feist6bd2a022018-03-13 12:30:58 -070068 FOUND,
69 MATCH_ONE
James Feist3cb5fec2018-01-23 14:41:51 -080070};
James Feista465ccc2019-02-08 12:51:01 -080071const static boost::container::flat_map<const char*, probe_type_codes, cmp_str>
James Feist3cb5fec2018-01-23 14:41:51 -080072 PROBE_TYPES{{{"FALSE", probe_type_codes::FALSE_T},
73 {"TRUE", probe_type_codes::TRUE_T},
74 {"AND", probe_type_codes::AND},
75 {"OR", probe_type_codes::OR},
James Feist6bd2a022018-03-13 12:30:58 -070076 {"FOUND", probe_type_codes::FOUND},
77 {"MATCH_ONE", probe_type_codes::MATCH_ONE}}};
James Feist3cb5fec2018-01-23 14:41:51 -080078
James Feist41334262019-03-25 13:30:20 -070079static constexpr std::array<const char*, 5> settableInterfaces = {
80 "FanProfile", "Pid", "Pid.Zone", "Stepwise", "Thresholds"};
James Feist68500ff2018-08-08 15:40:42 -070081using JsonVariantType =
James Feist338b8a72019-03-01 10:16:45 -080082 std::variant<std::vector<std::string>, std::vector<double>, std::string,
83 int64_t, uint64_t, double, int32_t, uint32_t, int16_t,
84 uint16_t, uint8_t, bool>;
James Feist3cb5fec2018-01-23 14:41:51 -080085using GetSubTreeType = std::vector<
86 std::pair<std::string,
87 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
88
89using ManagedObjectType = boost::container::flat_map<
James Feist8f2710a2018-05-09 17:18:55 -070090 sdbusplus::message::object_path,
James Feist3cb5fec2018-01-23 14:41:51 -080091 boost::container::flat_map<
92 std::string,
James Feist8f2710a2018-05-09 17:18:55 -070093 boost::container::flat_map<std::string, BasicVariantType>>>;
James Feist3cb5fec2018-01-23 14:41:51 -080094
95boost::container::flat_map<
96 std::string,
James Feist8f2710a2018-05-09 17:18:55 -070097 std::vector<boost::container::flat_map<std::string, BasicVariantType>>>
James Feist3cb5fec2018-01-23 14:41:51 -080098 DBUS_PROBE_OBJECTS;
99std::vector<std::string> PASSED_PROBES;
100
101// 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
Johnathan Mantey2015f752019-03-26 15:22:31 -0700105const std::regex ILLEGAL_DBUS_PATH_REGEX("[^A-Za-z0-9_.]");
106const std::regex ILLEGAL_DBUS_MEMBER_REGEX("[^A-Za-z0-9_]");
James Feist1b2e2242018-01-30 13:45:19 -0800107
James Feista465ccc2019-02-08 12:51:01 -0800108void registerCallbacks(boost::asio::io_service& io,
109 std::vector<sdbusplus::bus::match::match>& dbusMatches,
110 nlohmann::json& systemConfiguration,
111 sdbusplus::asio::object_server& objServer);
James Feist75fdeeb2018-02-20 14:26:16 -0800112
James Feist3cb5fec2018-01-23 14:41:51 -0800113// calls the mapper to find all exposed objects of an interface type
114// and creates a vector<flat_map> that contains all the key value pairs
115// getManagedObjects
James Feist8f2710a2018-05-09 17:18:55 -0700116void findDbusObjects(std::shared_ptr<PerformProbe> probe,
117 std::shared_ptr<sdbusplus::asio::connection> connection,
James Feista465ccc2019-02-08 12:51:01 -0800118 std::string& interface)
James Feist3cb5fec2018-01-23 14:41:51 -0800119{
James Feist8f2710a2018-05-09 17:18:55 -0700120
121 // store reference to pending callbacks so we don't overwhelm services
122 static boost::container::flat_map<
123 std::string, std::vector<std::shared_ptr<PerformProbe>>>
124 pendingProbes;
125
126 if (DBUS_PROBE_OBJECTS[interface].size())
127 {
128 return;
129 }
130
131 // add shared_ptr to vector of Probes waiting for callback from a specific
132 // interface to keep alive while waiting for response
James Feista465ccc2019-02-08 12:51:01 -0800133 std::array<const char*, 1> objects = {interface.c_str()};
134 std::vector<std::shared_ptr<PerformProbe>>& pending =
James Feist8f2710a2018-05-09 17:18:55 -0700135 pendingProbes[interface];
136 auto iter = pending.emplace(pending.end(), probe);
137 // only allow first call to run to not overwhelm processes
138 if (iter != pending.begin())
139 {
140 return;
141 }
142
James Feist3cb5fec2018-01-23 14:41:51 -0800143 // find all connections in the mapper that expose a specific type
James Feist8f2710a2018-05-09 17:18:55 -0700144 connection->async_method_call(
James Feista465ccc2019-02-08 12:51:01 -0800145 [connection, interface, probe](boost::system::error_code& ec,
146 const GetSubTreeType& interfaceSubtree) {
James Feist0de40152018-07-25 11:56:12 -0700147 boost::container::flat_set<std::string> interfaceConnections;
James Feist8f2710a2018-05-09 17:18:55 -0700148 if (ec)
James Feist494155a2018-03-14 16:23:24 -0700149 {
James Feist0de40152018-07-25 11:56:12 -0700150 pendingProbes[interface].clear();
151 if (ec.value() == ENOENT)
James Feist8f2710a2018-05-09 17:18:55 -0700152 {
James Feist0de40152018-07-25 11:56:12 -0700153 return; // wasn't found by mapper
James Feist8f2710a2018-05-09 17:18:55 -0700154 }
James Feist0de40152018-07-25 11:56:12 -0700155 std::cerr << "Error communicating to mapper.\n";
156
157 // if we can't communicate to the mapper something is very wrong
158 std::exit(EXIT_FAILURE);
James Feist494155a2018-03-14 16:23:24 -0700159 }
James Feist8f2710a2018-05-09 17:18:55 -0700160 else
James Feist3cb5fec2018-01-23 14:41:51 -0800161 {
James Feista465ccc2019-02-08 12:51:01 -0800162 for (auto& object : interfaceSubtree)
James Feist8f2710a2018-05-09 17:18:55 -0700163 {
James Feista465ccc2019-02-08 12:51:01 -0800164 for (auto& connPair : object.second)
James Feist8f2710a2018-05-09 17:18:55 -0700165 {
James Feist0eb40352019-04-09 14:44:04 -0700166 interfaceConnections.insert(connPair.first);
James Feist8f2710a2018-05-09 17:18:55 -0700167 }
168 }
James Feist3cb5fec2018-01-23 14:41:51 -0800169 }
James Feist63845bf2019-01-24 12:19:51 -0800170 if (interfaceConnections.empty())
171 {
172 pendingProbes[interface].clear();
173 return;
174 }
James Feist8f2710a2018-05-09 17:18:55 -0700175 // get managed objects for all interfaces
James Feista465ccc2019-02-08 12:51:01 -0800176 for (const auto& conn : interfaceConnections)
James Feist8f2710a2018-05-09 17:18:55 -0700177 {
178 connection->async_method_call(
James Feist0de40152018-07-25 11:56:12 -0700179 [conn,
James Feist98132792019-07-09 13:29:09 -0700180 interface](boost::system::error_code& errc,
James Feista465ccc2019-02-08 12:51:01 -0800181 const ManagedObjectType& managedInterface) {
James Feist98132792019-07-09 13:29:09 -0700182 if (errc)
James Feist8f2710a2018-05-09 17:18:55 -0700183 {
184 std::cerr
185 << "error getting managed object for device "
186 << conn << "\n";
187 pendingProbes[interface].clear();
188 return;
189 }
James Feista465ccc2019-02-08 12:51:01 -0800190 for (auto& interfaceManagedObj : managedInterface)
James Feist8f2710a2018-05-09 17:18:55 -0700191 {
192 auto ifaceObjFind =
193 interfaceManagedObj.second.find(interface);
194 if (ifaceObjFind !=
195 interfaceManagedObj.second.end())
196 {
197 std::vector<boost::container::flat_map<
James Feista465ccc2019-02-08 12:51:01 -0800198 std::string, BasicVariantType>>&
199 dbusObject = DBUS_PROBE_OBJECTS[interface];
James Feist8f2710a2018-05-09 17:18:55 -0700200 dbusObject.emplace_back(ifaceObjFind->second);
201 }
202 }
203 pendingProbes[interface].clear();
204 },
205 conn.c_str(), "/", "org.freedesktop.DBus.ObjectManager",
206 "GetManagedObjects");
207 }
208 },
209 "xyz.openbmc_project.ObjectMapper",
210 "/xyz/openbmc_project/object_mapper",
James Feist5131ac22018-10-25 12:22:23 -0700211 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", MAX_MAPPER_DEPTH,
James Feist8f2710a2018-05-09 17:18:55 -0700212 objects);
James Feist3cb5fec2018-01-23 14:41:51 -0800213}
James Feist8f2710a2018-05-09 17:18:55 -0700214// probes dbus interface dictionary for a key with a value that matches a regex
James Feist3cb5fec2018-01-23 14:41:51 -0800215bool probeDbus(
James Feista465ccc2019-02-08 12:51:01 -0800216 const std::string& interface,
217 const std::map<std::string, nlohmann::json>& matches,
James Feistf1b14142019-04-10 15:22:09 -0700218 std::vector<std::optional<
219 boost::container::flat_map<std::string, BasicVariantType>>>& devices,
James Feista465ccc2019-02-08 12:51:01 -0800220 bool& foundProbe)
James Feist3cb5fec2018-01-23 14:41:51 -0800221{
James Feista465ccc2019-02-08 12:51:01 -0800222 std::vector<boost::container::flat_map<std::string, BasicVariantType>>&
223 dbusObject = DBUS_PROBE_OBJECTS[interface];
James Feist3cb5fec2018-01-23 14:41:51 -0800224 if (dbusObject.empty())
225 {
James Feist8f2710a2018-05-09 17:18:55 -0700226 foundProbe = false;
227 return false;
James Feist3cb5fec2018-01-23 14:41:51 -0800228 }
229 foundProbe = true;
230
231 bool foundMatch = false;
James Feista465ccc2019-02-08 12:51:01 -0800232 for (auto& device : dbusObject)
James Feist3cb5fec2018-01-23 14:41:51 -0800233 {
234 bool deviceMatches = true;
James Feista465ccc2019-02-08 12:51:01 -0800235 for (auto& match : matches)
James Feist3cb5fec2018-01-23 14:41:51 -0800236 {
237 auto deviceValue = device.find(match.first);
238 if (deviceValue != device.end())
239 {
240 switch (match.second.type())
241 {
James Feist9eb0b582018-04-27 12:15:46 -0700242 case nlohmann::json::value_t::string:
James Feist3cb5fec2018-01-23 14:41:51 -0800243 {
James Feist9eb0b582018-04-27 12:15:46 -0700244 std::regex search(match.second.get<std::string>());
James Feist98132792019-07-09 13:29:09 -0700245 std::smatch regMatch;
James Feist9eb0b582018-04-27 12:15:46 -0700246
247 // convert value to string respresentation
James Feista465ccc2019-02-08 12:51:01 -0800248 std::string probeValue = std::visit(
James Feist8f2710a2018-05-09 17:18:55 -0700249 VariantToStringVisitor(), deviceValue->second);
James Feist98132792019-07-09 13:29:09 -0700250 if (!std::regex_search(probeValue, regMatch, search))
James Feist9eb0b582018-04-27 12:15:46 -0700251 {
252 deviceMatches = false;
253 break;
254 }
James Feist3cb5fec2018-01-23 14:41:51 -0800255 break;
256 }
James Feist9eb0b582018-04-27 12:15:46 -0700257 case nlohmann::json::value_t::boolean:
258 case nlohmann::json::value_t::number_unsigned:
James Feist3cb5fec2018-01-23 14:41:51 -0800259 {
James Feista465ccc2019-02-08 12:51:01 -0800260 unsigned int probeValue = std::visit(
James Feist9eb0b582018-04-27 12:15:46 -0700261 VariantToUnsignedIntVisitor(), deviceValue->second);
James Feist3cb5fec2018-01-23 14:41:51 -0800262
James Feist9eb0b582018-04-27 12:15:46 -0700263 if (probeValue != match.second.get<unsigned int>())
264 {
265 deviceMatches = false;
266 }
267 break;
James Feist3cb5fec2018-01-23 14:41:51 -0800268 }
James Feist9eb0b582018-04-27 12:15:46 -0700269 case nlohmann::json::value_t::number_integer:
270 {
James Feista465ccc2019-02-08 12:51:01 -0800271 int probeValue = std::visit(VariantToIntVisitor(),
272 deviceValue->second);
James Feist3cb5fec2018-01-23 14:41:51 -0800273
James Feist9eb0b582018-04-27 12:15:46 -0700274 if (probeValue != match.second.get<int>())
275 {
276 deviceMatches = false;
277 }
278 break;
James Feist3cb5fec2018-01-23 14:41:51 -0800279 }
James Feist9eb0b582018-04-27 12:15:46 -0700280 case nlohmann::json::value_t::number_float:
281 {
James Feista465ccc2019-02-08 12:51:01 -0800282 float probeValue = std::visit(VariantToFloatVisitor(),
283 deviceValue->second);
James Feist9eb0b582018-04-27 12:15:46 -0700284
285 if (probeValue != match.second.get<float>())
286 {
287 deviceMatches = false;
288 }
289 break;
290 }
James Feist0eb40352019-04-09 14:44:04 -0700291 default:
292 {
293 std::cerr << "unexpected dbus probe type "
294 << match.second.type_name() << "\n";
295 }
James Feist3cb5fec2018-01-23 14:41:51 -0800296 }
297 }
298 else
299 {
300 deviceMatches = false;
301 break;
302 }
303 }
304 if (deviceMatches)
305 {
James Feistf5125b02019-06-06 11:27:43 -0700306 devices.emplace_back(device);
James Feist3cb5fec2018-01-23 14:41:51 -0800307 foundMatch = true;
308 deviceMatches = false; // for next iteration
309 }
310 }
311 return foundMatch;
312}
313
314// default probe entry point, iterates a list looking for specific types to
315// call specific probe functions
316bool probe(
James Feista465ccc2019-02-08 12:51:01 -0800317 const std::vector<std::string>& probeCommand,
James Feistf1b14142019-04-10 15:22:09 -0700318 std::vector<std::optional<
319 boost::container::flat_map<std::string, BasicVariantType>>>& foundDevs)
James Feist3cb5fec2018-01-23 14:41:51 -0800320{
321 const static std::regex command(R"(\((.*)\))");
322 std::smatch match;
323 bool ret = false;
James Feist6bd2a022018-03-13 12:30:58 -0700324 bool matchOne = false;
James Feist3cb5fec2018-01-23 14:41:51 -0800325 bool cur = true;
326 probe_type_codes lastCommand = probe_type_codes::FALSE_T;
James Feist54a0dca2019-06-26 10:34:54 -0700327 bool first = true;
James Feist3cb5fec2018-01-23 14:41:51 -0800328
James Feista465ccc2019-02-08 12:51:01 -0800329 for (auto& probe : probeCommand)
James Feist3cb5fec2018-01-23 14:41:51 -0800330 {
331 bool foundProbe = false;
James Feista465ccc2019-02-08 12:51:01 -0800332 boost::container::flat_map<const char*, probe_type_codes,
James Feist3cb5fec2018-01-23 14:41:51 -0800333 cmp_str>::const_iterator probeType;
334
335 for (probeType = PROBE_TYPES.begin(); probeType != PROBE_TYPES.end();
336 probeType++)
337 {
338 if (probe.find(probeType->first) != std::string::npos)
339 {
340 foundProbe = true;
341 break;
342 }
343 }
344 if (foundProbe)
345 {
346 switch (probeType->second)
347 {
James Feist9eb0b582018-04-27 12:15:46 -0700348 case probe_type_codes::FALSE_T:
James Feist3cb5fec2018-01-23 14:41:51 -0800349 {
James Feiste31e00a2019-07-24 10:45:43 -0700350 cur = false;
351 break;
James Feist3cb5fec2018-01-23 14:41:51 -0800352 }
James Feist9eb0b582018-04-27 12:15:46 -0700353 case probe_type_codes::TRUE_T:
354 {
James Feiste31e00a2019-07-24 10:45:43 -0700355 cur = true;
356 break;
James Feist9eb0b582018-04-27 12:15:46 -0700357 }
358 case probe_type_codes::MATCH_ONE:
359 {
360 // set current value to last, this probe type shouldn't
361 // affect the outcome
362 cur = ret;
363 matchOne = true;
364 break;
365 }
366 /*case probe_type_codes::AND:
367 break;
368 case probe_type_codes::OR:
369 break;
370 // these are no-ops until the last command switch
371 */
372 case probe_type_codes::FOUND:
373 {
374 if (!std::regex_search(probe, match, command))
375 {
James Feist0eb40352019-04-09 14:44:04 -0700376 std::cerr << "found probe syntax error " << probe
James Feist9eb0b582018-04-27 12:15:46 -0700377 << "\n";
378 return false;
379 }
380 std::string commandStr = *(match.begin() + 1);
381 boost::replace_all(commandStr, "'", "");
382 cur = (std::find(PASSED_PROBES.begin(), PASSED_PROBES.end(),
383 commandStr) != PASSED_PROBES.end());
384 break;
385 }
James Feist0eb40352019-04-09 14:44:04 -0700386 default:
387 {
388 break;
389 }
James Feist3cb5fec2018-01-23 14:41:51 -0800390 }
391 }
392 // look on dbus for object
393 else
394 {
395 if (!std::regex_search(probe, match, command))
396 {
James Feist0eb40352019-04-09 14:44:04 -0700397 std::cerr << "dbus probe syntax error " << probe << "\n";
James Feist3cb5fec2018-01-23 14:41:51 -0800398 return false;
399 }
400 std::string commandStr = *(match.begin() + 1);
401 // convert single ticks and single slashes into legal json
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700402 boost::replace_all(commandStr, "'", "\"");
James Feist3f8a2782018-02-12 09:24:42 -0800403 boost::replace_all(commandStr, R"(\)", R"(\\)");
James Feist3cb5fec2018-01-23 14:41:51 -0800404 auto json = nlohmann::json::parse(commandStr, nullptr, false);
405 if (json.is_discarded())
406 {
James Feist0eb40352019-04-09 14:44:04 -0700407 std::cerr << "dbus command syntax error " << commandStr << "\n";
James Feist3cb5fec2018-01-23 14:41:51 -0800408 return false;
409 }
410 // we can match any (string, variant) property. (string, string)
411 // does a regex
412 std::map<std::string, nlohmann::json> dbusProbeMap =
413 json.get<std::map<std::string, nlohmann::json>>();
414 auto findStart = probe.find("(");
415 if (findStart == std::string::npos)
416 {
417 return false;
418 }
419 std::string probeInterface = probe.substr(0, findStart);
420 cur =
421 probeDbus(probeInterface, dbusProbeMap, foundDevs, foundProbe);
422 }
423
424 // some functions like AND and OR only take affect after the
425 // fact
James Feist54a0dca2019-06-26 10:34:54 -0700426 if (lastCommand == probe_type_codes::AND)
James Feist3cb5fec2018-01-23 14:41:51 -0800427 {
James Feist54a0dca2019-06-26 10:34:54 -0700428 ret = cur && ret;
429 }
430 else if (lastCommand == probe_type_codes::OR)
431 {
432 ret = cur || ret;
433 }
434
435 if (first)
436 {
437 ret = cur;
438 first = false;
James Feist3cb5fec2018-01-23 14:41:51 -0800439 }
440 lastCommand = probeType != PROBE_TYPES.end()
441 ? probeType->second
442 : probe_type_codes::FALSE_T;
James Feist3cb5fec2018-01-23 14:41:51 -0800443 }
444
445 // probe passed, but empty device
James Feist3cb5fec2018-01-23 14:41:51 -0800446 if (ret && foundDevs.size() == 0)
447 {
James Feistf1b14142019-04-10 15:22:09 -0700448 foundDevs.emplace_back(std::nullopt);
James Feist3cb5fec2018-01-23 14:41:51 -0800449 }
James Feist0eb40352019-04-09 14:44:04 -0700450 if (matchOne && ret)
James Feist6bd2a022018-03-13 12:30:58 -0700451 {
James Feist71f295f2019-06-20 13:35:12 -0700452 // match the last one
453 auto last = foundDevs.back();
James Feist0eb40352019-04-09 14:44:04 -0700454 foundDevs.clear();
James Feist71f295f2019-06-20 13:35:12 -0700455
456 foundDevs.emplace_back(std::move(last));
James Feist6bd2a022018-03-13 12:30:58 -0700457 }
James Feist3cb5fec2018-01-23 14:41:51 -0800458 return ret;
459}
James Feist8f2710a2018-05-09 17:18:55 -0700460// this class finds the needed dbus fields and on destruction runs the probe
461struct PerformProbe : std::enable_shared_from_this<PerformProbe>
462{
James Feist3cb5fec2018-01-23 14:41:51 -0800463
James Feist8f2710a2018-05-09 17:18:55 -0700464 PerformProbe(
James Feista465ccc2019-02-08 12:51:01 -0800465 const std::vector<std::string>& probeCommand,
James Feistf1b14142019-04-10 15:22:09 -0700466 std::function<void(std::vector<std::optional<boost::container::flat_map<
467 std::string, BasicVariantType>>>&)>&& callback) :
James Feist8f2710a2018-05-09 17:18:55 -0700468 _probeCommand(probeCommand),
469 _callback(std::move(callback))
470 {
471 }
472 ~PerformProbe()
473 {
James Feistf1b14142019-04-10 15:22:09 -0700474 std::vector<std::optional<
475 boost::container::flat_map<std::string, BasicVariantType>>>
James Feist0eb40352019-04-09 14:44:04 -0700476 foundDevs;
477 if (probe(_probeCommand, foundDevs))
James Feist8f2710a2018-05-09 17:18:55 -0700478 {
James Feist0eb40352019-04-09 14:44:04 -0700479 _callback(foundDevs);
James Feist8f2710a2018-05-09 17:18:55 -0700480 }
481 }
482 void run()
483 {
484 // parse out dbus probes by discarding other probe types
James Feist8f2710a2018-05-09 17:18:55 -0700485
James Feista465ccc2019-02-08 12:51:01 -0800486 for (std::string& probe : _probeCommand)
James Feist8f2710a2018-05-09 17:18:55 -0700487 {
488 bool found = false;
James Feista465ccc2019-02-08 12:51:01 -0800489 boost::container::flat_map<const char*, probe_type_codes,
James Feist8f2710a2018-05-09 17:18:55 -0700490 cmp_str>::const_iterator probeType;
491 for (probeType = PROBE_TYPES.begin();
492 probeType != PROBE_TYPES.end(); probeType++)
493 {
494 if (probe.find(probeType->first) != std::string::npos)
495 {
496 found = true;
497 break;
498 }
499 }
500 if (found)
501 {
502 continue;
503 }
504 // syntax requires probe before first open brace
505 auto findStart = probe.find("(");
506 std::string interface = probe.substr(0, findStart);
507
508 findDbusObjects(shared_from_this(), SYSTEM_BUS, interface);
509 }
510 }
511 std::vector<std::string> _probeCommand;
James Feistf1b14142019-04-10 15:22:09 -0700512 std::function<void(std::vector<std::optional<boost::container::flat_map<
513 std::string, BasicVariantType>>>&)>
James Feist8f2710a2018-05-09 17:18:55 -0700514 _callback;
James Feist8f2710a2018-05-09 17:18:55 -0700515};
516
517// writes output files to persist data
James Feista465ccc2019-02-08 12:51:01 -0800518bool writeJsonFiles(const nlohmann::json& systemConfiguration)
James Feist1b2e2242018-01-30 13:45:19 -0800519{
James Feist1df06a42019-04-11 14:23:04 -0700520 std::filesystem::create_directory(configurationOutDir);
521 std::ofstream output(currentConfiguration);
James Feistbb43d022018-06-12 15:44:33 -0700522 if (!output.good())
523 {
524 return false;
525 }
James Feist1b2e2242018-01-30 13:45:19 -0800526 output << systemConfiguration.dump(4);
527 output.close();
James Feistbb43d022018-06-12 15:44:33 -0700528 return true;
James Feist8f2710a2018-05-09 17:18:55 -0700529}
James Feist1b2e2242018-01-30 13:45:19 -0800530
James Feist97a63f12018-05-17 13:50:57 -0700531template <typename JsonType>
James Feista465ccc2019-02-08 12:51:01 -0800532bool setJsonFromPointer(const std::string& ptrStr, const JsonType& value,
533 nlohmann::json& systemConfiguration)
James Feist97a63f12018-05-17 13:50:57 -0700534{
535 try
536 {
537 nlohmann::json::json_pointer ptr(ptrStr);
James Feista465ccc2019-02-08 12:51:01 -0800538 nlohmann::json& ref = systemConfiguration[ptr];
James Feist97a63f12018-05-17 13:50:57 -0700539 ref = value;
540 return true;
541 }
James Feist98132792019-07-09 13:29:09 -0700542 catch (const std::out_of_range&)
James Feist97a63f12018-05-17 13:50:57 -0700543 {
544 return false;
545 }
546}
James Feistbb43d022018-06-12 15:44:33 -0700547
James Feistebcc26b2019-03-22 12:30:43 -0700548// template function to add array as dbus property
549template <typename PropertyType>
550void addArrayToDbus(const std::string& name, const nlohmann::json& array,
551 sdbusplus::asio::dbus_interface* iface,
552 sdbusplus::asio::PropertyPermission permission,
553 nlohmann::json& systemConfiguration,
554 const std::string& jsonPointerString)
555{
556 std::vector<PropertyType> values;
557 for (const auto& property : array)
558 {
559 auto ptr = property.get_ptr<const PropertyType*>();
560 if (ptr != nullptr)
561 {
562 values.emplace_back(*ptr);
563 }
564 }
565
566 if (permission == sdbusplus::asio::PropertyPermission::readOnly)
567 {
568 iface->register_property(name, values);
569 }
570 else
571 {
572 iface->register_property(
573 name, values,
574 [&systemConfiguration,
575 jsonPointerString{std::string(jsonPointerString)}](
576 const std::vector<PropertyType>& newVal,
577 std::vector<PropertyType>& val) {
578 val = newVal;
579 if (!setJsonFromPointer(jsonPointerString, val,
580 systemConfiguration))
581 {
582 std::cerr << "error setting json field\n";
583 return -1;
584 }
585 if (!writeJsonFiles(systemConfiguration))
586 {
587 std::cerr << "error setting json file\n";
588 return -1;
589 }
590 return 1;
591 });
592 }
593}
594
James Feistbb43d022018-06-12 15:44:33 -0700595template <typename PropertyType>
James Feista465ccc2019-02-08 12:51:01 -0800596void addProperty(const std::string& propertyName, const PropertyType& value,
597 sdbusplus::asio::dbus_interface* iface,
598 nlohmann::json& systemConfiguration,
599 const std::string& jsonPointerString,
James Feistbb43d022018-06-12 15:44:33 -0700600 sdbusplus::asio::PropertyPermission permission)
601{
602 if (permission == sdbusplus::asio::PropertyPermission::readOnly)
603 {
604 iface->register_property(propertyName, value);
605 return;
606 }
James Feist68500ff2018-08-08 15:40:42 -0700607 iface->register_property(
608 propertyName, value,
609 [&systemConfiguration,
610 jsonPointerString{std::string(jsonPointerString)}](
James Feista465ccc2019-02-08 12:51:01 -0800611 const PropertyType& newVal, PropertyType& val) {
James Feist68500ff2018-08-08 15:40:42 -0700612 val = newVal;
613 if (!setJsonFromPointer(jsonPointerString, val,
614 systemConfiguration))
615 {
616 std::cerr << "error setting json field\n";
617 return -1;
618 }
James Feistc6248a52018-08-14 10:09:45 -0700619 if (!writeJsonFiles(systemConfiguration))
James Feist68500ff2018-08-08 15:40:42 -0700620 {
621 std::cerr << "error setting json file\n";
James Feistc6248a52018-08-14 10:09:45 -0700622 return -1;
623 }
624 return 1;
625 });
626}
627
628void createDeleteObjectMethod(
James Feista465ccc2019-02-08 12:51:01 -0800629 const std::string& jsonPointerPath,
630 const std::shared_ptr<sdbusplus::asio::dbus_interface>& iface,
631 sdbusplus::asio::object_server& objServer,
632 nlohmann::json& systemConfiguration)
James Feistc6248a52018-08-14 10:09:45 -0700633{
634 std::weak_ptr<sdbusplus::asio::dbus_interface> interface = iface;
635 iface->register_method(
636 "Delete", [&objServer, &systemConfiguration, interface,
637 jsonPointerPath{std::string(jsonPointerPath)}]() {
James Feist98132792019-07-09 13:29:09 -0700638 std::shared_ptr<sdbusplus::asio::dbus_interface> dbusInterface =
James Feistc6248a52018-08-14 10:09:45 -0700639 interface.lock();
James Feist98132792019-07-09 13:29:09 -0700640 if (!dbusInterface)
James Feistc6248a52018-08-14 10:09:45 -0700641 {
642 // this technically can't happen as the pointer is pointing to
643 // us
644 throw DBusInternalError();
645 }
646 nlohmann::json::json_pointer ptr(jsonPointerPath);
James Feist98132792019-07-09 13:29:09 -0700647 if (!objServer.remove_interface(dbusInterface))
James Feistc6248a52018-08-14 10:09:45 -0700648 {
649 std::cerr << "Can't delete interface " << jsonPointerPath
650 << "\n";
651 throw DBusInternalError();
652 }
653 systemConfiguration[ptr] = nullptr;
654
655 if (!writeJsonFiles(systemConfiguration))
656 {
657 std::cerr << "error setting json file\n";
658 throw DBusInternalError();
James Feist68500ff2018-08-08 15:40:42 -0700659 }
James Feistbb43d022018-06-12 15:44:33 -0700660 return -1;
James Feist68500ff2018-08-08 15:40:42 -0700661 });
James Feistbb43d022018-06-12 15:44:33 -0700662}
663
James Feist1b2e2242018-01-30 13:45:19 -0800664// adds simple json types to interface's properties
James Feistbb43d022018-06-12 15:44:33 -0700665void populateInterfaceFromJson(
James Feista465ccc2019-02-08 12:51:01 -0800666 nlohmann::json& systemConfiguration, const std::string& jsonPointerPath,
667 std::shared_ptr<sdbusplus::asio::dbus_interface>& iface,
668 nlohmann::json& dict, sdbusplus::asio::object_server& objServer,
James Feistbb43d022018-06-12 15:44:33 -0700669 sdbusplus::asio::PropertyPermission permission =
670 sdbusplus::asio::PropertyPermission::readOnly)
James Feist1b2e2242018-01-30 13:45:19 -0800671{
James Feista465ccc2019-02-08 12:51:01 -0800672 for (auto& dictPair : dict.items())
James Feist1b2e2242018-01-30 13:45:19 -0800673 {
James Feist8f2710a2018-05-09 17:18:55 -0700674 auto type = dictPair.value().type();
675 bool array = false;
676 if (dictPair.value().type() == nlohmann::json::value_t::array)
677 {
678 array = true;
679 if (!dictPair.value().size())
680 {
681 continue;
682 }
683 type = dictPair.value()[0].type();
684 bool isLegal = true;
James Feista465ccc2019-02-08 12:51:01 -0800685 for (const auto& arrayItem : dictPair.value())
James Feist8f2710a2018-05-09 17:18:55 -0700686 {
687 if (arrayItem.type() != type)
688 {
689 isLegal = false;
690 break;
691 }
692 }
693 if (!isLegal)
694 {
695 std::cerr << "dbus format error" << dictPair.value() << "\n";
696 continue;
697 }
James Feista218ddb2019-04-11 14:01:31 -0700698 }
699 if (type == nlohmann::json::value_t::object)
700 {
701 continue; // handled elsewhere
James Feist8f2710a2018-05-09 17:18:55 -0700702 }
James Feist97a63f12018-05-17 13:50:57 -0700703 std::string key = jsonPointerPath + "/" + dictPair.key();
James Feistbb43d022018-06-12 15:44:33 -0700704 if (permission == sdbusplus::asio::PropertyPermission::readWrite)
705 {
706 // all setable numbers are doubles as it is difficult to always
707 // create a configuration file with all whole numbers as decimals
708 // i.e. 1.0
James Feistebcc26b2019-03-22 12:30:43 -0700709 if (array)
710 {
711 if (dictPair.value()[0].is_number())
712 {
713 type = nlohmann::json::value_t::number_float;
714 }
715 }
716 else if (dictPair.value().is_number())
James Feistbb43d022018-06-12 15:44:33 -0700717 {
718 type = nlohmann::json::value_t::number_float;
719 }
720 }
721
James Feist8f2710a2018-05-09 17:18:55 -0700722 switch (type)
James Feist1b2e2242018-01-30 13:45:19 -0800723 {
James Feist9eb0b582018-04-27 12:15:46 -0700724 case (nlohmann::json::value_t::boolean):
725 {
James Feist8f2710a2018-05-09 17:18:55 -0700726 if (array)
727 {
728 // todo: array of bool isn't detected correctly by
729 // sdbusplus, change it to numbers
730 addArrayToDbus<uint64_t>(dictPair.key(), dictPair.value(),
James Feistebcc26b2019-03-22 12:30:43 -0700731 iface.get(), permission,
732 systemConfiguration, key);
James Feist8f2710a2018-05-09 17:18:55 -0700733 }
James Feistbb43d022018-06-12 15:44:33 -0700734
James Feist97a63f12018-05-17 13:50:57 -0700735 else
736 {
James Feistbb43d022018-06-12 15:44:33 -0700737 addProperty(dictPair.key(), dictPair.value().get<bool>(),
James Feistc6248a52018-08-14 10:09:45 -0700738 iface.get(), systemConfiguration, key,
739 permission);
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_integer):
744 {
James Feist8f2710a2018-05-09 17:18:55 -0700745 if (array)
746 {
747 addArrayToDbus<int64_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(), dictPair.value().get<int64_t>(),
James Feistc6248a52018-08-14 10:09:45 -0700754 iface.get(), systemConfiguration, key,
James Feistbb43d022018-06-12 15:44:33 -0700755 sdbusplus::asio::PropertyPermission::readOnly);
James Feist97a63f12018-05-17 13:50:57 -0700756 }
James Feist9eb0b582018-04-27 12:15:46 -0700757 break;
758 }
759 case (nlohmann::json::value_t::number_unsigned):
760 {
James Feist8f2710a2018-05-09 17:18:55 -0700761 if (array)
762 {
763 addArrayToDbus<uint64_t>(dictPair.key(), dictPair.value(),
James Feistebcc26b2019-03-22 12:30:43 -0700764 iface.get(), permission,
765 systemConfiguration, key);
James Feist97a63f12018-05-17 13:50:57 -0700766 }
767 else
768 {
James Feistbb43d022018-06-12 15:44:33 -0700769 addProperty(dictPair.key(),
James Feistc6248a52018-08-14 10:09:45 -0700770 dictPair.value().get<uint64_t>(), iface.get(),
James Feistbb43d022018-06-12 15:44:33 -0700771 systemConfiguration, key,
772 sdbusplus::asio::PropertyPermission::readOnly);
James Feist97a63f12018-05-17 13:50:57 -0700773 }
James Feist9eb0b582018-04-27 12:15:46 -0700774 break;
775 }
776 case (nlohmann::json::value_t::number_float):
777 {
James Feist8f2710a2018-05-09 17:18:55 -0700778 if (array)
779 {
780 addArrayToDbus<double>(dictPair.key(), dictPair.value(),
James Feistebcc26b2019-03-22 12:30:43 -0700781 iface.get(), permission,
782 systemConfiguration, key);
James Feist8f2710a2018-05-09 17:18:55 -0700783 }
James Feistbb43d022018-06-12 15:44:33 -0700784
James Feist97a63f12018-05-17 13:50:57 -0700785 else
786 {
James Feistbb43d022018-06-12 15:44:33 -0700787 addProperty(dictPair.key(), dictPair.value().get<double>(),
James Feistc6248a52018-08-14 10:09:45 -0700788 iface.get(), systemConfiguration, key,
789 permission);
James Feist97a63f12018-05-17 13:50:57 -0700790 }
James Feist9eb0b582018-04-27 12:15:46 -0700791 break;
792 }
793 case (nlohmann::json::value_t::string):
794 {
James Feist8f2710a2018-05-09 17:18:55 -0700795 if (array)
796 {
James Feistebcc26b2019-03-22 12:30:43 -0700797 addArrayToDbus<std::string>(
798 dictPair.key(), dictPair.value(), iface.get(),
799 permission, systemConfiguration, key);
James Feist97a63f12018-05-17 13:50:57 -0700800 }
801 else
802 {
James Feistc6248a52018-08-14 10:09:45 -0700803 addProperty(
804 dictPair.key(), dictPair.value().get<std::string>(),
805 iface.get(), systemConfiguration, key, permission);
James Feist97a63f12018-05-17 13:50:57 -0700806 }
James Feist9eb0b582018-04-27 12:15:46 -0700807 break;
808 }
James Feist0eb40352019-04-09 14:44:04 -0700809 default:
810 {
James Feista218ddb2019-04-11 14:01:31 -0700811 std::cerr << "Unexpected json type in system configuration "
812 << dictPair.key() << ": "
813 << dictPair.value().type_name() << "\n";
James Feist0eb40352019-04-09 14:44:04 -0700814 break;
815 }
James Feist1b2e2242018-01-30 13:45:19 -0800816 }
817 }
James Feistc6248a52018-08-14 10:09:45 -0700818 if (permission == sdbusplus::asio::PropertyPermission::readWrite)
819 {
820 createDeleteObjectMethod(jsonPointerPath, iface, objServer,
821 systemConfiguration);
822 }
James Feist8f2710a2018-05-09 17:18:55 -0700823 iface->initialize();
James Feist1b2e2242018-01-30 13:45:19 -0800824}
825
James Feista465ccc2019-02-08 12:51:01 -0800826sdbusplus::asio::PropertyPermission getPermission(const std::string& interface)
James Feistc6248a52018-08-14 10:09:45 -0700827{
828 return std::find(settableInterfaces.begin(), settableInterfaces.end(),
829 interface) != settableInterfaces.end()
830 ? sdbusplus::asio::PropertyPermission::readWrite
831 : sdbusplus::asio::PropertyPermission::readOnly;
832}
833
James Feista465ccc2019-02-08 12:51:01 -0800834void createAddObjectMethod(const std::string& jsonPointerPath,
835 const std::string& path,
836 nlohmann::json& systemConfiguration,
837 sdbusplus::asio::object_server& objServer)
James Feist68500ff2018-08-08 15:40:42 -0700838{
839 auto iface = objServer.add_interface(path, "xyz.openbmc_project.AddObject");
840
841 iface->register_method(
842 "AddObject",
843 [&systemConfiguration, &objServer,
844 jsonPointerPath{std::string(jsonPointerPath)},
845 path{std::string(path)}](
James Feista465ccc2019-02-08 12:51:01 -0800846 const boost::container::flat_map<std::string, JsonVariantType>&
847 data) {
James Feist68500ff2018-08-08 15:40:42 -0700848 nlohmann::json::json_pointer ptr(jsonPointerPath);
James Feista465ccc2019-02-08 12:51:01 -0800849 nlohmann::json& base = systemConfiguration[ptr];
James Feist68500ff2018-08-08 15:40:42 -0700850 auto findExposes = base.find("Exposes");
851
852 if (findExposes == base.end())
853 {
854 throw std::invalid_argument("Entity must have children.");
855 }
856
857 // this will throw invalid-argument to sdbusplus if invalid json
858 nlohmann::json newData{};
James Feista465ccc2019-02-08 12:51:01 -0800859 for (const auto& item : data)
James Feist68500ff2018-08-08 15:40:42 -0700860 {
James Feista465ccc2019-02-08 12:51:01 -0800861 nlohmann::json& newJson = newData[item.first];
862 std::visit([&newJson](auto&& val) { newJson = std::move(val); },
863 item.second);
James Feist68500ff2018-08-08 15:40:42 -0700864 }
865
866 auto findName = newData.find("Name");
867 auto findType = newData.find("Type");
868 if (findName == newData.end() || findType == newData.end())
869 {
870 throw std::invalid_argument("AddObject missing Name or Type");
871 }
James Feista465ccc2019-02-08 12:51:01 -0800872 const std::string* type = findType->get_ptr<const std::string*>();
873 const std::string* name = findName->get_ptr<const std::string*>();
James Feist68500ff2018-08-08 15:40:42 -0700874 if (type == nullptr || name == nullptr)
875 {
876 throw std::invalid_argument("Type and Name must be a string.");
877 }
878
879 size_t lastIndex = 0;
880 // we add in the "exposes"
881 for (; lastIndex < findExposes->size(); lastIndex++)
882 {
883 if (findExposes->at(lastIndex)["Name"] == *name &&
884 findExposes->at(lastIndex)["Type"] == *type)
885 {
886 throw std::invalid_argument(
887 "Field already in JSON, not adding");
888 }
889 lastIndex++;
890 }
891
892 std::ifstream schemaFile(std::string(schemaDirectory) + "/" +
893 *type + ".json");
894 // todo(james) we might want to also make a list of 'can add'
895 // interfaces but for now I think the assumption if there is a
896 // schema avaliable that it is allowed to update is fine
897 if (!schemaFile.good())
898 {
899 throw std::invalid_argument(
900 "No schema avaliable, cannot validate.");
901 }
902 nlohmann::json schema =
903 nlohmann::json::parse(schemaFile, nullptr, false);
904 if (schema.is_discarded())
905 {
906 std::cerr << "Schema not legal" << *type << ".json\n";
907 throw DBusInternalError();
908 }
909 if (!validateJson(schema, newData))
910 {
911 throw std::invalid_argument("Data does not match schema");
912 }
913
James Feist16a02f22019-05-13 15:21:37 -0700914 findExposes->push_back(newData);
James Feist68500ff2018-08-08 15:40:42 -0700915 if (!writeJsonFiles(systemConfiguration))
916 {
917 std::cerr << "Error writing json files\n";
918 throw DBusInternalError();
919 }
920 std::string dbusName = *name;
921
922 std::regex_replace(dbusName.begin(), dbusName.begin(),
Johnathan Mantey2015f752019-03-26 15:22:31 -0700923 dbusName.end(), ILLEGAL_DBUS_MEMBER_REGEX, "_");
James Feist98132792019-07-09 13:29:09 -0700924 auto interface = objServer.add_interface(
James Feist68500ff2018-08-08 15:40:42 -0700925 path + "/" + dbusName,
926 "xyz.openbmc_project.Configuration." + *type);
927 // permission is read-write, as since we just created it, must be
928 // runtime modifiable
929 populateInterfaceFromJson(
930 systemConfiguration,
James Feist16a02f22019-05-13 15:21:37 -0700931 jsonPointerPath + "/Exposes/" + std::to_string(lastIndex),
James Feist98132792019-07-09 13:29:09 -0700932 interface, newData, objServer,
James Feist68500ff2018-08-08 15:40:42 -0700933 sdbusplus::asio::PropertyPermission::readWrite);
James Feist68500ff2018-08-08 15:40:42 -0700934 });
935 iface->initialize();
936}
937
James Feista465ccc2019-02-08 12:51:01 -0800938void postToDbus(const nlohmann::json& newConfiguration,
939 nlohmann::json& systemConfiguration,
940 sdbusplus::asio::object_server& objServer)
James Feist75fdeeb2018-02-20 14:26:16 -0800941
James Feist1b2e2242018-01-30 13:45:19 -0800942{
James Feist97a63f12018-05-17 13:50:57 -0700943 // iterate through boards
James Feista465ccc2019-02-08 12:51:01 -0800944 for (auto& boardPair : newConfiguration.items())
James Feist1b2e2242018-01-30 13:45:19 -0800945 {
James Feistf1b14142019-04-10 15:22:09 -0700946 std::string boardKey = boardPair.value()["Name"];
947 std::string jsonPointerPath = "/" + boardPair.key();
James Feist97a63f12018-05-17 13:50:57 -0700948 // loop through newConfiguration, but use values from system
949 // configuration to be able to modify via dbus later
James Feistf1b14142019-04-10 15:22:09 -0700950 auto boardValues = systemConfiguration[boardPair.key()];
James Feistd63d18a2018-07-19 15:23:45 -0700951 auto findBoardType = boardValues.find("Type");
James Feist1b2e2242018-01-30 13:45:19 -0800952 std::string boardType;
953 if (findBoardType != boardValues.end() &&
954 findBoardType->type() == nlohmann::json::value_t::string)
955 {
956 boardType = findBoardType->get<std::string>();
957 std::regex_replace(boardType.begin(), boardType.begin(),
Johnathan Mantey2015f752019-03-26 15:22:31 -0700958 boardType.end(), ILLEGAL_DBUS_MEMBER_REGEX, "_");
James Feist1b2e2242018-01-30 13:45:19 -0800959 }
960 else
961 {
962 std::cerr << "Unable to find type for " << boardKey
963 << " reverting to Chassis.\n";
964 boardType = "Chassis";
965 }
James Feist11be6672018-04-06 14:05:32 -0700966 std::string boardtypeLower = boost::algorithm::to_lower_copy(boardType);
James Feist1b2e2242018-01-30 13:45:19 -0800967
968 std::regex_replace(boardKey.begin(), boardKey.begin(), boardKey.end(),
Johnathan Mantey2015f752019-03-26 15:22:31 -0700969 ILLEGAL_DBUS_MEMBER_REGEX, "_");
James Feist11be6672018-04-06 14:05:32 -0700970 std::string boardName = "/xyz/openbmc_project/inventory/system/" +
971 boardtypeLower + "/" + boardKey;
James Feist1b2e2242018-01-30 13:45:19 -0800972
James Feist8f2710a2018-05-09 17:18:55 -0700973 auto inventoryIface = objServer.add_interface(
974 boardName, "xyz.openbmc_project.Inventory.Item");
James Feist68500ff2018-08-08 15:40:42 -0700975
James Feist8f2710a2018-05-09 17:18:55 -0700976 auto boardIface = objServer.add_interface(
977 boardName, "xyz.openbmc_project.Inventory.Item." + boardType);
James Feist11be6672018-04-06 14:05:32 -0700978
James Feist68500ff2018-08-08 15:40:42 -0700979 createAddObjectMethod(jsonPointerPath, boardName, systemConfiguration,
980 objServer);
981
James Feist97a63f12018-05-17 13:50:57 -0700982 populateInterfaceFromJson(systemConfiguration, jsonPointerPath,
James Feistc6248a52018-08-14 10:09:45 -0700983 boardIface, boardValues, objServer);
James Feist97a63f12018-05-17 13:50:57 -0700984 jsonPointerPath += "/";
985 // iterate through board properties
James Feista465ccc2019-02-08 12:51:01 -0800986 for (auto& boardField : boardValues.items())
James Feist11be6672018-04-06 14:05:32 -0700987 {
988 if (boardField.value().type() == nlohmann::json::value_t::object)
989 {
James Feist8f2710a2018-05-09 17:18:55 -0700990 auto iface =
991 objServer.add_interface(boardName, boardField.key());
James Feistc6248a52018-08-14 10:09:45 -0700992 populateInterfaceFromJson(systemConfiguration,
993 jsonPointerPath + boardField.key(),
994 iface, boardField.value(), objServer);
James Feist11be6672018-04-06 14:05:32 -0700995 }
996 }
James Feist97a63f12018-05-17 13:50:57 -0700997
James Feist1e3e6982018-08-03 16:09:28 -0700998 auto exposes = boardValues.find("Exposes");
James Feist1b2e2242018-01-30 13:45:19 -0800999 if (exposes == boardValues.end())
1000 {
1001 continue;
1002 }
James Feist97a63f12018-05-17 13:50:57 -07001003 // iterate through exposes
James Feist1e3e6982018-08-03 16:09:28 -07001004 jsonPointerPath += "Exposes/";
James Feist97a63f12018-05-17 13:50:57 -07001005
1006 // store the board level pointer so we can modify it on the way down
1007 std::string jsonPointerPathBoard = jsonPointerPath;
1008 size_t exposesIndex = -1;
James Feista465ccc2019-02-08 12:51:01 -08001009 for (auto& item : *exposes)
James Feist1b2e2242018-01-30 13:45:19 -08001010 {
James Feist97a63f12018-05-17 13:50:57 -07001011 exposesIndex++;
1012 jsonPointerPath = jsonPointerPathBoard;
1013 jsonPointerPath += std::to_string(exposesIndex);
1014
James Feistd63d18a2018-07-19 15:23:45 -07001015 auto findName = item.find("Name");
James Feist1b2e2242018-01-30 13:45:19 -08001016 if (findName == item.end())
1017 {
1018 std::cerr << "cannot find name in field " << item << "\n";
1019 continue;
1020 }
James Feist1e3e6982018-08-03 16:09:28 -07001021 auto findStatus = item.find("Status");
James Feist1b2e2242018-01-30 13:45:19 -08001022 // if status is not found it is assumed to be status = 'okay'
1023 if (findStatus != item.end())
1024 {
1025 if (*findStatus == "disabled")
1026 {
1027 continue;
1028 }
1029 }
James Feistd63d18a2018-07-19 15:23:45 -07001030 auto findType = item.find("Type");
James Feist1b2e2242018-01-30 13:45:19 -08001031 std::string itemType;
1032 if (findType != item.end())
1033 {
1034 itemType = findType->get<std::string>();
1035 std::regex_replace(itemType.begin(), itemType.begin(),
Johnathan Mantey2015f752019-03-26 15:22:31 -07001036 itemType.end(), ILLEGAL_DBUS_PATH_REGEX,
1037 "_");
James Feist1b2e2242018-01-30 13:45:19 -08001038 }
1039 else
1040 {
1041 itemType = "unknown";
1042 }
1043 std::string itemName = findName->get<std::string>();
1044 std::regex_replace(itemName.begin(), itemName.begin(),
Johnathan Mantey2015f752019-03-26 15:22:31 -07001045 itemName.end(), ILLEGAL_DBUS_MEMBER_REGEX, "_");
James Feistc6248a52018-08-14 10:09:45 -07001046
James Feist8f2710a2018-05-09 17:18:55 -07001047 auto itemIface = objServer.add_interface(
1048 boardName + "/" + itemName,
James Feist1b2e2242018-01-30 13:45:19 -08001049 "xyz.openbmc_project.Configuration." + itemType);
1050
James Feist97a63f12018-05-17 13:50:57 -07001051 populateInterfaceFromJson(systemConfiguration, jsonPointerPath,
James Feistc6248a52018-08-14 10:09:45 -07001052 itemIface, item, objServer,
1053 getPermission(itemType));
James Feist1b2e2242018-01-30 13:45:19 -08001054
James Feista465ccc2019-02-08 12:51:01 -08001055 for (auto& objectPair : item.items())
James Feist1b2e2242018-01-30 13:45:19 -08001056 {
James Feist97a63f12018-05-17 13:50:57 -07001057 jsonPointerPath = jsonPointerPathBoard +
1058 std::to_string(exposesIndex) + "/" +
1059 objectPair.key();
James Feist1b2e2242018-01-30 13:45:19 -08001060 if (objectPair.value().type() ==
1061 nlohmann::json::value_t::object)
1062 {
James Feist8f2710a2018-05-09 17:18:55 -07001063 auto objectIface = objServer.add_interface(
1064 boardName + "/" + itemName,
James Feist1b2e2242018-01-30 13:45:19 -08001065 "xyz.openbmc_project.Configuration." + itemType + "." +
James Feist8f2710a2018-05-09 17:18:55 -07001066 objectPair.key());
James Feist97a63f12018-05-17 13:50:57 -07001067
1068 populateInterfaceFromJson(
James Feistc6248a52018-08-14 10:09:45 -07001069 systemConfiguration, jsonPointerPath, objectIface,
1070 objectPair.value(), objServer, getPermission(itemType));
James Feist1b2e2242018-01-30 13:45:19 -08001071 }
1072 else if (objectPair.value().type() ==
1073 nlohmann::json::value_t::array)
1074 {
1075 size_t index = 0;
James Feist8f2710a2018-05-09 17:18:55 -07001076 if (!objectPair.value().size())
James Feist1b2e2242018-01-30 13:45:19 -08001077 {
James Feist8f2710a2018-05-09 17:18:55 -07001078 continue;
1079 }
1080 bool isLegal = true;
1081 auto type = objectPair.value()[0].type();
1082 if (type != nlohmann::json::value_t::object)
1083 {
1084 continue;
1085 }
1086
1087 // verify legal json
James Feista465ccc2019-02-08 12:51:01 -08001088 for (const auto& arrayItem : objectPair.value())
James Feist8f2710a2018-05-09 17:18:55 -07001089 {
1090 if (arrayItem.type() != type)
James Feist1b2e2242018-01-30 13:45:19 -08001091 {
James Feist8f2710a2018-05-09 17:18:55 -07001092 isLegal = false;
James Feist1b2e2242018-01-30 13:45:19 -08001093 break;
1094 }
James Feist8f2710a2018-05-09 17:18:55 -07001095 }
1096 if (!isLegal)
1097 {
1098 std::cerr << "dbus format error" << objectPair.value()
1099 << "\n";
1100 break;
1101 }
1102
James Feista465ccc2019-02-08 12:51:01 -08001103 for (auto& arrayItem : objectPair.value())
James Feist8f2710a2018-05-09 17:18:55 -07001104 {
James Feist97a63f12018-05-17 13:50:57 -07001105
James Feist8f2710a2018-05-09 17:18:55 -07001106 auto objectIface = objServer.add_interface(
1107 boardName + "/" + itemName,
James Feist1b2e2242018-01-30 13:45:19 -08001108 "xyz.openbmc_project.Configuration." + itemType +
James Feistbb43d022018-06-12 15:44:33 -07001109 "." + objectPair.key() + std::to_string(index));
James Feistc6248a52018-08-14 10:09:45 -07001110 populateInterfaceFromJson(
1111 systemConfiguration,
1112 jsonPointerPath + "/" + std::to_string(index),
1113 objectIface, arrayItem, objServer,
1114 getPermission(objectPair.key()));
James Feistbb43d022018-06-12 15:44:33 -07001115 index++;
James Feist1b2e2242018-01-30 13:45:19 -08001116 }
1117 }
1118 }
1119 }
1120 }
1121}
1122
James Feist8f2710a2018-05-09 17:18:55 -07001123// reads json files out of the filesystem
James Feista465ccc2019-02-08 12:51:01 -08001124bool findJsonFiles(std::list<nlohmann::json>& configurations)
James Feist3cb5fec2018-01-23 14:41:51 -08001125{
1126 // find configuration files
Ed Tanous072e25d2018-12-16 21:45:20 -08001127 std::vector<std::filesystem::path> jsonPaths;
Johnathan Mantey2015f752019-03-26 15:22:31 -07001128 if (!findFiles(std::filesystem::path(configurationDirectory), R"(.*\.json)",
1129 jsonPaths))
James Feist3cb5fec2018-01-23 14:41:51 -08001130 {
1131 std::cerr << "Unable to find any configuration files in "
James Feistb4383f42018-08-06 16:54:10 -07001132 << configurationDirectory << "\n";
James Feist75fdeeb2018-02-20 14:26:16 -08001133 return false;
James Feist3cb5fec2018-01-23 14:41:51 -08001134 }
James Feistb4383f42018-08-06 16:54:10 -07001135
1136 std::ifstream schemaStream(std::string(schemaDirectory) + "/" +
1137 globalSchema);
1138 if (!schemaStream.good())
1139 {
1140 std::cerr
1141 << "Cannot open schema file, cannot validate JSON, exiting\n\n";
1142 std::exit(EXIT_FAILURE);
Ed Tanous072e25d2018-12-16 21:45:20 -08001143 return false;
James Feistb4383f42018-08-06 16:54:10 -07001144 }
1145 nlohmann::json schema = nlohmann::json::parse(schemaStream, nullptr, false);
1146 if (schema.is_discarded())
1147 {
1148 std::cerr
1149 << "Illegal schema file detected, cannot validate JSON, exiting\n";
1150 std::exit(EXIT_FAILURE);
Ed Tanous072e25d2018-12-16 21:45:20 -08001151 return false;
James Feistb4383f42018-08-06 16:54:10 -07001152 }
1153
James Feista465ccc2019-02-08 12:51:01 -08001154 for (auto& jsonPath : jsonPaths)
James Feist3cb5fec2018-01-23 14:41:51 -08001155 {
1156 std::ifstream jsonStream(jsonPath.c_str());
1157 if (!jsonStream.good())
1158 {
1159 std::cerr << "unable to open " << jsonPath.string() << "\n";
1160 continue;
1161 }
1162 auto data = nlohmann::json::parse(jsonStream, nullptr, false);
1163 if (data.is_discarded())
1164 {
1165 std::cerr << "syntax error in " << jsonPath.string() << "\n";
1166 continue;
1167 }
James Feist8da99192019-01-24 08:20:16 -08001168 /*
1169 * todo(james): reenable this once less things are in flight
1170 *
James Feistb4383f42018-08-06 16:54:10 -07001171 if (!validateJson(schema, data))
1172 {
1173 std::cerr << "Error validating " << jsonPath.string() << "\n";
1174 continue;
1175 }
James Feist8da99192019-01-24 08:20:16 -08001176 */
James Feistb4383f42018-08-06 16:54:10 -07001177
James Feist3cb5fec2018-01-23 14:41:51 -08001178 if (data.type() == nlohmann::json::value_t::array)
1179 {
James Feista465ccc2019-02-08 12:51:01 -08001180 for (auto& d : data)
James Feist3cb5fec2018-01-23 14:41:51 -08001181 {
1182 configurations.emplace_back(d);
1183 }
1184 }
1185 else
1186 {
1187 configurations.emplace_back(data);
1188 }
1189 }
Ed Tanous072e25d2018-12-16 21:45:20 -08001190 return true;
James Feist75fdeeb2018-02-20 14:26:16 -08001191}
James Feist3cb5fec2018-01-23 14:41:51 -08001192
James Feist8f2710a2018-05-09 17:18:55 -07001193struct PerformScan : std::enable_shared_from_this<PerformScan>
James Feist75fdeeb2018-02-20 14:26:16 -08001194{
James Feist75fdeeb2018-02-20 14:26:16 -08001195
James Feista465ccc2019-02-08 12:51:01 -08001196 PerformScan(nlohmann::json& systemConfiguration,
1197 std::list<nlohmann::json>& configurations,
1198 std::function<void(void)>&& callback) :
James Feist8f2710a2018-05-09 17:18:55 -07001199 _systemConfiguration(systemConfiguration),
1200 _configurations(configurations), _callback(std::move(callback))
James Feist3cb5fec2018-01-23 14:41:51 -08001201 {
James Feist8f2710a2018-05-09 17:18:55 -07001202 }
1203 void run()
1204 {
1205 for (auto it = _configurations.begin(); it != _configurations.end();)
James Feist3cb5fec2018-01-23 14:41:51 -08001206 {
James Feist1e3e6982018-08-03 16:09:28 -07001207 auto findProbe = it->find("Probe");
James Feistd63d18a2018-07-19 15:23:45 -07001208 auto findName = it->find("Name");
James Feist3cb5fec2018-01-23 14:41:51 -08001209
James Feist1b2e2242018-01-30 13:45:19 -08001210 nlohmann::json probeCommand;
1211 // check for poorly formatted fields, probe must be an array
1212 if (findProbe == it->end())
James Feist3cb5fec2018-01-23 14:41:51 -08001213 {
1214 std::cerr << "configuration file missing probe:\n " << *it
1215 << "\n";
James Feist8f2710a2018-05-09 17:18:55 -07001216 it = _configurations.erase(it);
1217 continue;
James Feist3cb5fec2018-01-23 14:41:51 -08001218 }
James Feist1b2e2242018-01-30 13:45:19 -08001219 else if ((*findProbe).type() != nlohmann::json::value_t::array)
James Feist3cb5fec2018-01-23 14:41:51 -08001220 {
1221 probeCommand = nlohmann::json::array();
1222 probeCommand.push_back(*findProbe);
1223 }
1224 else
1225 {
1226 probeCommand = *findProbe;
1227 }
James Feist1b2e2242018-01-30 13:45:19 -08001228
1229 if (findName == it->end())
1230 {
1231 std::cerr << "configuration file missing name:\n " << *it
1232 << "\n";
James Feist8f2710a2018-05-09 17:18:55 -07001233 it = _configurations.erase(it);
1234 continue;
James Feist1b2e2242018-01-30 13:45:19 -08001235 }
James Feistf1b14142019-04-10 15:22:09 -07001236 std::string probeName = *findName;
James Feist1b2e2242018-01-30 13:45:19 -08001237
James Feistf1b14142019-04-10 15:22:09 -07001238 if (std::find(PASSED_PROBES.begin(), PASSED_PROBES.end(),
1239 probeName) != PASSED_PROBES.end())
James Feist3cb5fec2018-01-23 14:41:51 -08001240 {
James Feist8f2710a2018-05-09 17:18:55 -07001241 it = _configurations.erase(it);
1242 continue;
1243 }
James Feistf1b14142019-04-10 15:22:09 -07001244 nlohmann::json* recordPtr = &(*it);
James Feist3cb5fec2018-01-23 14:41:51 -08001245
James Feist8f2710a2018-05-09 17:18:55 -07001246 // store reference to this to children to makes sure we don't get
1247 // destroyed too early
1248 auto thisRef = shared_from_this();
1249 auto p = std::make_shared<PerformProbe>(
1250 probeCommand,
James Feistf1b14142019-04-10 15:22:09 -07001251 [&, recordPtr, probeName,
1252 thisRef](std::vector<std::optional<boost::container::flat_map<
1253 std::string, BasicVariantType>>>& foundDevices) {
James Feist8f2710a2018-05-09 17:18:55 -07001254 _passed = true;
James Feist3cb5fec2018-01-23 14:41:51 -08001255
James Feistf1b14142019-04-10 15:22:09 -07001256 PASSED_PROBES.push_back(probeName);
James Feist8f2710a2018-05-09 17:18:55 -07001257 size_t foundDeviceIdx = 0;
1258
James Feista465ccc2019-02-08 12:51:01 -08001259 for (auto& foundDevice : foundDevices)
James Feist3cb5fec2018-01-23 14:41:51 -08001260 {
James Feistf5125b02019-06-06 11:27:43 -07001261 nlohmann::json record = *recordPtr;
James Feistf1b14142019-04-10 15:22:09 -07001262 std::string recordName;
1263 size_t hash = 0;
1264 if (foundDevice)
James Feist3cb5fec2018-01-23 14:41:51 -08001265 {
James Feistf1b14142019-04-10 15:22:09 -07001266 // use an array so alphabetical order from the
1267 // flat_map is maintained
1268 auto device = nlohmann::json::array();
1269 for (auto& devPair : *foundDevice)
1270 {
1271 device.push_back(devPair.first);
1272 std::visit(
1273 [&device](auto&& v) {
1274 device.push_back(v);
1275 },
1276 devPair.second);
1277 }
1278 hash = std::hash<std::string>{}(probeName +
1279 device.dump());
1280 // hashes are hard to distinguish, use the
1281 // non-hashed version if we want debug
1282 if constexpr (DEBUG)
1283 {
1284 recordName = probeName + device.dump();
1285 }
1286 else
1287 {
1288 recordName = std::to_string(hash);
1289 }
James Feist8f2710a2018-05-09 17:18:55 -07001290 }
James Feistf1b14142019-04-10 15:22:09 -07001291 else
1292 {
1293 recordName = probeName;
1294 }
1295
James Feist1df06a42019-04-11 14:23:04 -07001296 auto fromLastJson = lastJson.find(recordName);
1297 if (fromLastJson != lastJson.end())
1298 {
1299 // keep user changes
1300 _systemConfiguration[recordName] = *fromLastJson;
1301 continue;
1302 }
1303
James Feistf1b14142019-04-10 15:22:09 -07001304 // insert into configuration temporarily to be able to
1305 // reference ourselves
1306
1307 _systemConfiguration[recordName] = record;
1308
1309 if (foundDevice)
1310 {
1311 for (auto keyPair = record.begin();
1312 keyPair != record.end(); keyPair++)
1313 {
1314 templateCharReplace(keyPair, *foundDevice,
1315 foundDeviceIdx);
1316 }
1317 }
Patrick Ventureb8293c02019-08-09 14:12:47 -07001318
James Feistf1b14142019-04-10 15:22:09 -07001319 auto findExpose = record.find("Exposes");
1320 if (findExpose == record.end())
James Feist8f2710a2018-05-09 17:18:55 -07001321 {
Patrick Ventureb8293c02019-08-09 14:12:47 -07001322 _systemConfiguration[recordName] = record;
1323 logDeviceAdded(record);
1324 foundDeviceIdx++;
James Feist8f2710a2018-05-09 17:18:55 -07001325 continue;
1326 }
Patrick Ventureb8293c02019-08-09 14:12:47 -07001327
James Feista465ccc2019-02-08 12:51:01 -08001328 for (auto& expose : *findExpose)
James Feist8f2710a2018-05-09 17:18:55 -07001329 {
1330 for (auto keyPair = expose.begin();
1331 keyPair != expose.end(); keyPair++)
James Feist3cb5fec2018-01-23 14:41:51 -08001332 {
James Feist1b2e2242018-01-30 13:45:19 -08001333
James Feist8f2710a2018-05-09 17:18:55 -07001334 // fill in template characters with devices
1335 // found
James Feistf1b14142019-04-10 15:22:09 -07001336 if (foundDevice)
1337 {
1338 templateCharReplace(keyPair, *foundDevice,
1339 foundDeviceIdx);
1340 }
James Feist8f2710a2018-05-09 17:18:55 -07001341 // special case bind
James Feist1e3e6982018-08-03 16:09:28 -07001342 if (boost::starts_with(keyPair.key(), "Bind"))
James Feist8f2710a2018-05-09 17:18:55 -07001343 {
1344 if (keyPair.value().type() !=
1345 nlohmann::json::value_t::string)
James Feist3cb5fec2018-01-23 14:41:51 -08001346 {
James Feist8f2710a2018-05-09 17:18:55 -07001347 std::cerr << "bind_ value must be of "
1348 "type string "
1349 << keyPair.key() << "\n";
James Feist1b2e2242018-01-30 13:45:19 -08001350 continue;
1351 }
James Feist8f2710a2018-05-09 17:18:55 -07001352 bool foundBind = false;
1353 std::string bind = keyPair.key().substr(
James Feist1e3e6982018-08-03 16:09:28 -07001354 sizeof("Bind") - 1);
James Feistbe5425f2018-06-08 10:30:55 -07001355
James Feista465ccc2019-02-08 12:51:01 -08001356 for (auto& configurationPair :
James Feist8f2710a2018-05-09 17:18:55 -07001357 _systemConfiguration.items())
James Feist1b2e2242018-01-30 13:45:19 -08001358 {
James Feist1b2e2242018-01-30 13:45:19 -08001359
James Feist8f2710a2018-05-09 17:18:55 -07001360 auto configListFind =
1361 configurationPair.value().find(
James Feist1e3e6982018-08-03 16:09:28 -07001362 "Exposes");
James Feist8f2710a2018-05-09 17:18:55 -07001363
1364 if (configListFind ==
1365 configurationPair.value()
1366 .end() ||
1367 configListFind->type() !=
1368 nlohmann::json::value_t::array)
1369 {
1370 continue;
1371 }
James Feista465ccc2019-02-08 12:51:01 -08001372 for (auto& exposedObject :
James Feist8f2710a2018-05-09 17:18:55 -07001373 *configListFind)
1374 {
1375 std::string foundObjectName =
James Feistd63d18a2018-07-19 15:23:45 -07001376 (exposedObject)["Name"];
James Feist8f2710a2018-05-09 17:18:55 -07001377 if (boost::iequals(
1378 foundObjectName,
1379 keyPair.value()
1380 .get<std::string>()))
1381 {
James Feist1e3e6982018-08-03 16:09:28 -07001382 exposedObject["Status"] =
James Feist8f2710a2018-05-09 17:18:55 -07001383 "okay";
1384 expose[bind] = exposedObject;
1385
1386 foundBind = true;
1387 break;
1388 }
1389 }
1390 if (foundBind)
1391 {
James Feist3cb5fec2018-01-23 14:41:51 -08001392 break;
1393 }
1394 }
James Feist8f2710a2018-05-09 17:18:55 -07001395 if (!foundBind)
James Feist3cb5fec2018-01-23 14:41:51 -08001396 {
James Feist8f2710a2018-05-09 17:18:55 -07001397 std::cerr << "configuration file "
1398 "dependency error, "
1399 "could not find bind "
1400 << keyPair.value() << "\n";
James Feist3cb5fec2018-01-23 14:41:51 -08001401 }
1402 }
1403 }
1404 }
James Feistf1b14142019-04-10 15:22:09 -07001405 // overwrite ourselves with cleaned up version
1406 _systemConfiguration[recordName] = record;
James Feist1a996582019-05-14 15:10:06 -07001407
1408 logDeviceAdded(record);
James Feist1df06a42019-04-11 14:23:04 -07001409
James Feistf1b14142019-04-10 15:22:09 -07001410 foundDeviceIdx++;
James Feist3cb5fec2018-01-23 14:41:51 -08001411 }
James Feist8f2710a2018-05-09 17:18:55 -07001412 });
1413 p->run();
1414 it++;
James Feist3cb5fec2018-01-23 14:41:51 -08001415 }
1416 }
James Feist75fdeeb2018-02-20 14:26:16 -08001417
James Feist8f2710a2018-05-09 17:18:55 -07001418 ~PerformScan()
James Feist75fdeeb2018-02-20 14:26:16 -08001419 {
James Feist8f2710a2018-05-09 17:18:55 -07001420 if (_passed)
1421 {
1422 auto nextScan = std::make_shared<PerformScan>(
1423 _systemConfiguration, _configurations, std::move(_callback));
1424 nextScan->run();
1425 }
1426 else
1427 {
1428 _callback();
1429 }
1430 }
James Feista465ccc2019-02-08 12:51:01 -08001431 nlohmann::json& _systemConfiguration;
James Feist8f2710a2018-05-09 17:18:55 -07001432 std::list<nlohmann::json> _configurations;
1433 std::function<void(void)> _callback;
1434 std::vector<std::shared_ptr<PerformProbe>> _probes;
1435 bool _passed = false;
James Feist1df06a42019-04-11 14:23:04 -07001436 bool powerWasOn = isPowerOn();
James Feist8f2710a2018-05-09 17:18:55 -07001437};
James Feistc95cb142018-02-26 10:41:42 -08001438
James Feist1df06a42019-04-11 14:23:04 -07001439void startRemovedTimer(boost::asio::deadline_timer& timer,
James Feist1df06a42019-04-11 14:23:04 -07001440 nlohmann::json& systemConfiguration)
1441{
1442 static bool scannedPowerOff = false;
1443 static bool scannedPowerOn = false;
1444
James Feistfb00f392019-06-25 14:16:48 -07001445 if (systemConfiguration.empty() || lastJson.empty())
1446 {
1447 return; // not ready yet
1448 }
James Feist1df06a42019-04-11 14:23:04 -07001449 if (scannedPowerOn)
1450 {
1451 return;
1452 }
1453
1454 if (!isPowerOn() && scannedPowerOff)
1455 {
1456 return;
1457 }
1458
1459 timer.expires_from_now(boost::posix_time::seconds(10));
James Feist1a996582019-05-14 15:10:06 -07001460 timer.async_wait(
1461 [&systemConfiguration](const boost::system::error_code& ec) {
1462 if (ec == boost::asio::error::operation_aborted)
James Feist1df06a42019-04-11 14:23:04 -07001463 {
James Feist1a996582019-05-14 15:10:06 -07001464 // we were cancelled
1465 return;
1466 }
1467
1468 bool powerOff = !isPowerOn();
1469 for (const auto& item : lastJson.items())
1470 {
1471 if (systemConfiguration.find(item.key()) ==
1472 systemConfiguration.end())
James Feist1df06a42019-04-11 14:23:04 -07001473 {
James Feist1a996582019-05-14 15:10:06 -07001474 bool isDetectedPowerOn = false;
1475 auto powerState = item.value().find("PowerState");
1476 if (powerState != item.value().end())
James Feist1df06a42019-04-11 14:23:04 -07001477 {
James Feist1a996582019-05-14 15:10:06 -07001478 auto ptr = powerState->get_ptr<const std::string*>();
1479 if (ptr)
James Feist1df06a42019-04-11 14:23:04 -07001480 {
James Feist1a996582019-05-14 15:10:06 -07001481 if (*ptr == "On" || *ptr == "BiosPost")
1482 {
1483 isDetectedPowerOn = true;
1484 }
James Feist1df06a42019-04-11 14:23:04 -07001485 }
1486 }
James Feist1a996582019-05-14 15:10:06 -07001487 if (powerOff && isDetectedPowerOn)
1488 {
1489 // power not on yet, don't know if it's there or not
1490 continue;
1491 }
1492 if (!powerOff && scannedPowerOff && isDetectedPowerOn)
1493 {
1494 // already logged it when power was off
1495 continue;
1496 }
James Feist1df06a42019-04-11 14:23:04 -07001497
James Feist1a996582019-05-14 15:10:06 -07001498 logDeviceRemoved(item.value());
1499 }
James Feist1df06a42019-04-11 14:23:04 -07001500 }
James Feist1a996582019-05-14 15:10:06 -07001501 scannedPowerOff = true;
1502 if (!powerOff)
1503 {
1504 scannedPowerOn = true;
1505 }
1506 });
James Feist1df06a42019-04-11 14:23:04 -07001507}
1508
James Feist8f2710a2018-05-09 17:18:55 -07001509// main properties changed entry
1510void propertiesChangedCallback(
James Feista465ccc2019-02-08 12:51:01 -08001511 boost::asio::io_service& io,
1512 std::vector<sdbusplus::bus::match::match>& dbusMatches,
1513 nlohmann::json& systemConfiguration,
1514 sdbusplus::asio::object_server& objServer)
James Feist8f2710a2018-05-09 17:18:55 -07001515{
1516 static boost::asio::deadline_timer timer(io);
James Feist1df06a42019-04-11 14:23:04 -07001517 static bool timerRunning;
1518
1519 timerRunning = true;
James Feist8f2710a2018-05-09 17:18:55 -07001520 timer.expires_from_now(boost::posix_time::seconds(1));
1521
1522 // setup an async wait as we normally get flooded with new requests
James Feista465ccc2019-02-08 12:51:01 -08001523 timer.async_wait([&](const boost::system::error_code& ec) {
James Feist8f2710a2018-05-09 17:18:55 -07001524 if (ec == boost::asio::error::operation_aborted)
1525 {
1526 // we were cancelled
1527 return;
1528 }
1529 else if (ec)
1530 {
1531 std::cerr << "async wait error " << ec << "\n";
1532 return;
1533 }
James Feist1df06a42019-04-11 14:23:04 -07001534 timerRunning = false;
James Feist8f2710a2018-05-09 17:18:55 -07001535
1536 nlohmann::json oldConfiguration = systemConfiguration;
1537 DBUS_PROBE_OBJECTS.clear();
1538
1539 std::list<nlohmann::json> configurations;
1540 if (!findJsonFiles(configurations))
1541 {
1542 std::cerr << "cannot find json files\n";
1543 return;
1544 }
1545
1546 auto perfScan = std::make_shared<PerformScan>(
1547 systemConfiguration, configurations, [&, oldConfiguration]() {
1548 nlohmann::json newConfiguration = systemConfiguration;
James Feist4131aea2018-03-09 09:47:30 -08001549 for (auto it = newConfiguration.begin();
1550 it != newConfiguration.end();)
1551 {
1552 auto findKey = oldConfiguration.find(it.key());
1553 if (findKey != oldConfiguration.end())
1554 {
1555 it = newConfiguration.erase(it);
1556 }
1557 else
1558 {
1559 it++;
1560 }
1561 }
James Feist8f2710a2018-05-09 17:18:55 -07001562 registerCallbacks(io, dbusMatches, systemConfiguration,
1563 objServer);
1564 io.post([&, newConfiguration]() {
James Feist8f2710a2018-05-09 17:18:55 -07001565 loadOverlays(newConfiguration);
James Feistce4367c2018-10-16 09:19:57 -07001566
James Feistbb43d022018-06-12 15:44:33 -07001567 io.post([&]() {
1568 if (!writeJsonFiles(systemConfiguration))
1569 {
1570 std::cerr << "Error writing json files\n";
1571 }
1572 });
James Feist8f2710a2018-05-09 17:18:55 -07001573 io.post([&, newConfiguration]() {
James Feist97a63f12018-05-17 13:50:57 -07001574 postToDbus(newConfiguration, systemConfiguration,
1575 objServer);
James Feist1df06a42019-04-11 14:23:04 -07001576 if (!timerRunning)
1577 {
James Feist98132792019-07-09 13:29:09 -07001578 startRemovedTimer(timer, systemConfiguration);
James Feist1df06a42019-04-11 14:23:04 -07001579 }
James Feist8f2710a2018-05-09 17:18:55 -07001580 });
1581 });
1582 });
1583 perfScan->run();
1584 });
James Feist75fdeeb2018-02-20 14:26:16 -08001585}
1586
James Feista465ccc2019-02-08 12:51:01 -08001587void registerCallbacks(boost::asio::io_service& io,
1588 std::vector<sdbusplus::bus::match::match>& dbusMatches,
1589 nlohmann::json& systemConfiguration,
1590 sdbusplus::asio::object_server& objServer)
James Feist75fdeeb2018-02-20 14:26:16 -08001591{
1592 static boost::container::flat_set<std::string> watchedObjects;
1593
James Feista465ccc2019-02-08 12:51:01 -08001594 for (const auto& objectMap : DBUS_PROBE_OBJECTS)
James Feist75fdeeb2018-02-20 14:26:16 -08001595 {
1596 auto findObject = watchedObjects.find(objectMap.first);
1597 if (findObject != watchedObjects.end())
1598 {
1599 continue;
1600 }
James Feist8f2710a2018-05-09 17:18:55 -07001601 std::function<void(sdbusplus::message::message & message)>
1602 eventHandler =
James Feist75fdeeb2018-02-20 14:26:16 -08001603
James Feista465ccc2019-02-08 12:51:01 -08001604 [&](sdbusplus::message::message&) {
James Feist8f2710a2018-05-09 17:18:55 -07001605 propertiesChangedCallback(io, dbusMatches,
1606 systemConfiguration, objServer);
1607 };
1608
1609 sdbusplus::bus::match::match match(
James Feista465ccc2019-02-08 12:51:01 -08001610 static_cast<sdbusplus::bus::bus&>(*SYSTEM_BUS),
James Feist8f2710a2018-05-09 17:18:55 -07001611 "type='signal',member='PropertiesChanged',arg0='" +
1612 objectMap.first + "'",
1613 eventHandler);
1614 dbusMatches.emplace_back(std::move(match));
James Feist75fdeeb2018-02-20 14:26:16 -08001615 }
1616}
1617
James Feist98132792019-07-09 13:29:09 -07001618int main()
James Feist75fdeeb2018-02-20 14:26:16 -08001619{
1620 // setup connection to dbus
1621 boost::asio::io_service io;
James Feist8f2710a2018-05-09 17:18:55 -07001622 SYSTEM_BUS = std::make_shared<sdbusplus::asio::connection>(io);
James Feist75fdeeb2018-02-20 14:26:16 -08001623 SYSTEM_BUS->request_name("xyz.openbmc_project.EntityManager");
James Feist4131aea2018-03-09 09:47:30 -08001624
James Feist8f2710a2018-05-09 17:18:55 -07001625 sdbusplus::asio::object_server objServer(SYSTEM_BUS);
James Feistfd1264a2018-05-03 12:10:00 -07001626
James Feist8f2710a2018-05-09 17:18:55 -07001627 std::shared_ptr<sdbusplus::asio::dbus_interface> entityIface =
1628 objServer.add_interface("/xyz/openbmc_project/EntityManager",
1629 "xyz.openbmc_project.EntityManager");
James Feistfd1264a2018-05-03 12:10:00 -07001630
James Feist8f2710a2018-05-09 17:18:55 -07001631 std::shared_ptr<sdbusplus::asio::dbus_interface> inventoryIface =
1632 objServer.add_interface("/xyz/openbmc_project/inventory",
1633 "xyz.openbmc_project.Inventory.Manager");
James Feist4131aea2018-03-09 09:47:30 -08001634
1635 // to keep reference to the match / filter objects so they don't get
1636 // destroyed
James Feist8f2710a2018-05-09 17:18:55 -07001637 std::vector<sdbusplus::bus::match::match> dbusMatches;
1638
1639 nlohmann::json systemConfiguration = nlohmann::json::object();
1640
1641 inventoryIface->register_method(
James Feista465ccc2019-02-08 12:51:01 -08001642 "Notify",
1643 [](const boost::container::flat_map<
1644 std::string,
James Feist98132792019-07-09 13:29:09 -07001645 boost::container::flat_map<std::string, BasicVariantType>>&) {
1646 return;
1647 });
James Feist8f2710a2018-05-09 17:18:55 -07001648 inventoryIface->initialize();
1649
1650 io.post([&]() {
James Feistce4367c2018-10-16 09:19:57 -07001651#if OVERLAYS
James Feist8f2710a2018-05-09 17:18:55 -07001652 unloadAllOverlays();
James Feistce4367c2018-10-16 09:19:57 -07001653#endif
James Feist8f2710a2018-05-09 17:18:55 -07001654 propertiesChangedCallback(io, dbusMatches, systemConfiguration,
1655 objServer);
1656 });
James Feist4131aea2018-03-09 09:47:30 -08001657
James Feistfd1264a2018-05-03 12:10:00 -07001658 entityIface->register_method("ReScan", [&]() {
James Feist8f2710a2018-05-09 17:18:55 -07001659 propertiesChangedCallback(io, dbusMatches, systemConfiguration,
1660 objServer);
James Feist75fdeeb2018-02-20 14:26:16 -08001661 });
James Feist8f2710a2018-05-09 17:18:55 -07001662 entityIface->initialize();
1663
James Feist1df06a42019-04-11 14:23:04 -07001664 if (fwVersionIsSame())
1665 {
1666 if (std::filesystem::is_regular_file(currentConfiguration))
1667 {
1668 // this file could just be deleted, but it's nice for debug
1669 std::filesystem::create_directory(tempConfigDir);
1670 std::filesystem::remove(lastConfiguration);
1671 std::filesystem::copy(currentConfiguration, lastConfiguration);
1672 std::filesystem::remove(currentConfiguration);
1673
1674 std::ifstream jsonStream(lastConfiguration);
1675 if (jsonStream.good())
1676 {
1677 auto data = nlohmann::json::parse(jsonStream, nullptr, false);
1678 if (data.is_discarded())
1679 {
1680 std::cerr << "syntax error in " << lastConfiguration
1681 << "\n";
1682 }
1683 else
1684 {
1685 lastJson = std::move(data);
1686 }
1687 }
1688 else
1689 {
1690 std::cerr << "unable to open " << lastConfiguration << "\n";
1691 }
1692 }
1693 }
1694 else
1695 {
1696 // not an error, just logging at this level to make it in the journal
1697 std::cerr << "Clearing previous configuration\n";
1698 std::filesystem::remove(currentConfiguration);
1699 }
1700
1701 // some boards only show up after power is on, we want to not say they are
1702 // removed until the same state happens
1703 setupPowerMatch(SYSTEM_BUS);
1704
James Feist1b2e2242018-01-30 13:45:19 -08001705 io.run();
James Feist3cb5fec2018-01-23 14:41:51 -08001706
1707 return 0;
James Feist75fdeeb2018-02-20 14:26:16 -08001708}