blob: 57c9fda0cf51926684f5cb25259d3a1c42c71268 [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 Feistc95cb142018-02-26 10:41:42 -080019#include <Overlay.hpp>
James Feista465ccc2019-02-08 12:51:01 -080020#include <Utils.hpp>
21#include <VariantVisitors.hpp>
James Feist11be6672018-04-06 14:05:32 -070022#include <boost/algorithm/string/case_conv.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080023#include <boost/algorithm/string/predicate.hpp>
24#include <boost/algorithm/string/replace.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080025#include <boost/container/flat_map.hpp>
26#include <boost/container/flat_set.hpp>
James Feista465ccc2019-02-08 12:51:01 -080027#include <boost/lexical_cast.hpp>
James Feist637b3ef2019-04-15 16:35:30 -070028#include <filesystem>
James Feista465ccc2019-02-08 12:51:01 -080029#include <fstream>
30#include <iostream>
31#include <nlohmann/json.hpp>
32#include <regex>
33#include <sdbusplus/asio/connection.hpp>
34#include <sdbusplus/asio/object_server.hpp>
35#include <variant>
James Feist3cb5fec2018-01-23 14:41:51 -080036
James Feista465ccc2019-02-08 12:51:01 -080037constexpr const char* configurationDirectory = PACKAGE_DIR "configurations";
38constexpr const char* schemaDirectory = PACKAGE_DIR "configurations/schemas";
James Feist1df06a42019-04-11 14:23:04 -070039constexpr const char* tempConfigDir = "/tmp/configuration/";
40constexpr const char* lastConfiguration = "/tmp/configuration/last.json";
41constexpr const char* currentConfiguration = "/var/configuration/system.json";
James Feista465ccc2019-02-08 12:51:01 -080042constexpr const char* globalSchema = "global.json";
43constexpr const char* TEMPLATE_CHAR = "$";
James Feist8f2710a2018-05-09 17:18:55 -070044constexpr const int32_t MAX_MAPPER_DEPTH = 0;
James Feist3cb5fec2018-01-23 14:41:51 -080045
James Feistf1b14142019-04-10 15:22:09 -070046constexpr const bool DEBUG = false;
47
James Feist3cb5fec2018-01-23 14:41:51 -080048struct cmp_str
49{
James Feista465ccc2019-02-08 12:51:01 -080050 bool operator()(const char* a, const char* b) const
James Feist3cb5fec2018-01-23 14:41:51 -080051 {
52 return std::strcmp(a, b) < 0;
53 }
54};
55
James Feist8f2710a2018-05-09 17:18:55 -070056struct PerformProbe;
57
James Feist3cb5fec2018-01-23 14:41:51 -080058// underscore T for collison with dbus c api
59enum class probe_type_codes
60{
61 FALSE_T,
62 TRUE_T,
63 AND,
64 OR,
James Feist6bd2a022018-03-13 12:30:58 -070065 FOUND,
66 MATCH_ONE
James Feist3cb5fec2018-01-23 14:41:51 -080067};
James Feista465ccc2019-02-08 12:51:01 -080068const static boost::container::flat_map<const char*, probe_type_codes, cmp_str>
James Feist3cb5fec2018-01-23 14:41:51 -080069 PROBE_TYPES{{{"FALSE", probe_type_codes::FALSE_T},
70 {"TRUE", probe_type_codes::TRUE_T},
71 {"AND", probe_type_codes::AND},
72 {"OR", probe_type_codes::OR},
James Feist6bd2a022018-03-13 12:30:58 -070073 {"FOUND", probe_type_codes::FOUND},
74 {"MATCH_ONE", probe_type_codes::MATCH_ONE}}};
James Feist3cb5fec2018-01-23 14:41:51 -080075
James Feist41334262019-03-25 13:30:20 -070076static constexpr std::array<const char*, 5> settableInterfaces = {
77 "FanProfile", "Pid", "Pid.Zone", "Stepwise", "Thresholds"};
James Feist68500ff2018-08-08 15:40:42 -070078using JsonVariantType =
James Feist338b8a72019-03-01 10:16:45 -080079 std::variant<std::vector<std::string>, std::vector<double>, std::string,
80 int64_t, uint64_t, double, int32_t, uint32_t, int16_t,
81 uint16_t, uint8_t, bool>;
James Feist8f2710a2018-05-09 17:18:55 -070082using BasicVariantType =
James Feista465ccc2019-02-08 12:51:01 -080083 std::variant<std::string, int64_t, uint64_t, double, int32_t, uint32_t,
84 int16_t, uint16_t, uint8_t, bool>;
James Feist8f2710a2018-05-09 17:18:55 -070085
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
96boost::container::flat_map<
97 std::string,
James Feist8f2710a2018-05-09 17:18:55 -070098 std::vector<boost::container::flat_map<std::string, BasicVariantType>>>
James Feist3cb5fec2018-01-23 14:41:51 -080099 DBUS_PROBE_OBJECTS;
100std::vector<std::string> PASSED_PROBES;
101
102// todo: pass this through nicer
James Feist8f2710a2018-05-09 17:18:55 -0700103std::shared_ptr<sdbusplus::asio::connection> SYSTEM_BUS;
James Feist1df06a42019-04-11 14:23:04 -0700104static nlohmann::json lastJson;
James Feist3cb5fec2018-01-23 14:41:51 -0800105
Johnathan Mantey2015f752019-03-26 15:22:31 -0700106const std::regex ILLEGAL_DBUS_PATH_REGEX("[^A-Za-z0-9_.]");
107const std::regex ILLEGAL_DBUS_MEMBER_REGEX("[^A-Za-z0-9_]");
James Feist1b2e2242018-01-30 13:45:19 -0800108
James Feista465ccc2019-02-08 12:51:01 -0800109void registerCallbacks(boost::asio::io_service& io,
110 std::vector<sdbusplus::bus::match::match>& dbusMatches,
111 nlohmann::json& systemConfiguration,
112 sdbusplus::asio::object_server& objServer);
James Feist75fdeeb2018-02-20 14:26:16 -0800113
James Feist3cb5fec2018-01-23 14:41:51 -0800114// calls the mapper to find all exposed objects of an interface type
115// and creates a vector<flat_map> that contains all the key value pairs
116// getManagedObjects
James Feist8f2710a2018-05-09 17:18:55 -0700117void findDbusObjects(std::shared_ptr<PerformProbe> probe,
118 std::shared_ptr<sdbusplus::asio::connection> connection,
James Feista465ccc2019-02-08 12:51:01 -0800119 std::string& interface)
James Feist3cb5fec2018-01-23 14:41:51 -0800120{
James Feist8f2710a2018-05-09 17:18:55 -0700121
122 // store reference to pending callbacks so we don't overwhelm services
123 static boost::container::flat_map<
124 std::string, std::vector<std::shared_ptr<PerformProbe>>>
125 pendingProbes;
126
127 if (DBUS_PROBE_OBJECTS[interface].size())
128 {
129 return;
130 }
131
132 // add shared_ptr to vector of Probes waiting for callback from a specific
133 // interface to keep alive while waiting for response
James Feista465ccc2019-02-08 12:51:01 -0800134 std::array<const char*, 1> objects = {interface.c_str()};
135 std::vector<std::shared_ptr<PerformProbe>>& pending =
James Feist8f2710a2018-05-09 17:18:55 -0700136 pendingProbes[interface];
137 auto iter = pending.emplace(pending.end(), probe);
138 // only allow first call to run to not overwhelm processes
139 if (iter != pending.begin())
140 {
141 return;
142 }
143
James Feist3cb5fec2018-01-23 14:41:51 -0800144 // find all connections in the mapper that expose a specific type
James Feist8f2710a2018-05-09 17:18:55 -0700145 connection->async_method_call(
James Feista465ccc2019-02-08 12:51:01 -0800146 [connection, interface, probe](boost::system::error_code& ec,
147 const GetSubTreeType& interfaceSubtree) {
James Feist0de40152018-07-25 11:56:12 -0700148 boost::container::flat_set<std::string> interfaceConnections;
James Feist8f2710a2018-05-09 17:18:55 -0700149 if (ec)
James Feist494155a2018-03-14 16:23:24 -0700150 {
James Feist0de40152018-07-25 11:56:12 -0700151 pendingProbes[interface].clear();
152 if (ec.value() == ENOENT)
James Feist8f2710a2018-05-09 17:18:55 -0700153 {
James Feist0de40152018-07-25 11:56:12 -0700154 return; // wasn't found by mapper
James Feist8f2710a2018-05-09 17:18:55 -0700155 }
James Feist0de40152018-07-25 11:56:12 -0700156 std::cerr << "Error communicating to mapper.\n";
157
158 // if we can't communicate to the mapper something is very wrong
159 std::exit(EXIT_FAILURE);
James Feist494155a2018-03-14 16:23:24 -0700160 }
James Feist8f2710a2018-05-09 17:18:55 -0700161 else
James Feist3cb5fec2018-01-23 14:41:51 -0800162 {
James Feista465ccc2019-02-08 12:51:01 -0800163 for (auto& object : interfaceSubtree)
James Feist8f2710a2018-05-09 17:18:55 -0700164 {
James Feista465ccc2019-02-08 12:51:01 -0800165 for (auto& connPair : object.second)
James Feist8f2710a2018-05-09 17:18:55 -0700166 {
James Feist0eb40352019-04-09 14:44:04 -0700167 interfaceConnections.insert(connPair.first);
James Feist8f2710a2018-05-09 17:18:55 -0700168 }
169 }
James Feist3cb5fec2018-01-23 14:41:51 -0800170 }
James Feist63845bf2019-01-24 12:19:51 -0800171 if (interfaceConnections.empty())
172 {
173 pendingProbes[interface].clear();
174 return;
175 }
James Feist8f2710a2018-05-09 17:18:55 -0700176 // get managed objects for all interfaces
James Feista465ccc2019-02-08 12:51:01 -0800177 for (const auto& conn : interfaceConnections)
James Feist8f2710a2018-05-09 17:18:55 -0700178 {
179 connection->async_method_call(
James Feist0de40152018-07-25 11:56:12 -0700180 [conn,
James Feista465ccc2019-02-08 12:51:01 -0800181 interface](boost::system::error_code& ec,
182 const ManagedObjectType& managedInterface) {
James Feist8f2710a2018-05-09 17:18:55 -0700183 if (ec)
184 {
185 std::cerr
186 << "error getting managed object for device "
187 << conn << "\n";
188 pendingProbes[interface].clear();
189 return;
190 }
James Feista465ccc2019-02-08 12:51:01 -0800191 for (auto& interfaceManagedObj : managedInterface)
James Feist8f2710a2018-05-09 17:18:55 -0700192 {
193 auto ifaceObjFind =
194 interfaceManagedObj.second.find(interface);
195 if (ifaceObjFind !=
196 interfaceManagedObj.second.end())
197 {
198 std::vector<boost::container::flat_map<
James Feista465ccc2019-02-08 12:51:01 -0800199 std::string, BasicVariantType>>&
200 dbusObject = DBUS_PROBE_OBJECTS[interface];
James Feist8f2710a2018-05-09 17:18:55 -0700201 dbusObject.emplace_back(ifaceObjFind->second);
202 }
203 }
204 pendingProbes[interface].clear();
205 },
206 conn.c_str(), "/", "org.freedesktop.DBus.ObjectManager",
207 "GetManagedObjects");
208 }
209 },
210 "xyz.openbmc_project.ObjectMapper",
211 "/xyz/openbmc_project/object_mapper",
James Feist5131ac22018-10-25 12:22:23 -0700212 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", MAX_MAPPER_DEPTH,
James Feist8f2710a2018-05-09 17:18:55 -0700213 objects);
James Feist3cb5fec2018-01-23 14:41:51 -0800214}
James Feist8f2710a2018-05-09 17:18:55 -0700215// probes dbus interface dictionary for a key with a value that matches a regex
James Feist3cb5fec2018-01-23 14:41:51 -0800216bool probeDbus(
James Feista465ccc2019-02-08 12:51:01 -0800217 const std::string& interface,
218 const std::map<std::string, nlohmann::json>& matches,
James Feistf1b14142019-04-10 15:22:09 -0700219 std::vector<std::optional<
220 boost::container::flat_map<std::string, BasicVariantType>>>& devices,
James Feista465ccc2019-02-08 12:51:01 -0800221 bool& foundProbe)
James Feist3cb5fec2018-01-23 14:41:51 -0800222{
James Feista465ccc2019-02-08 12:51:01 -0800223 std::vector<boost::container::flat_map<std::string, BasicVariantType>>&
224 dbusObject = DBUS_PROBE_OBJECTS[interface];
James Feist3cb5fec2018-01-23 14:41:51 -0800225 if (dbusObject.empty())
226 {
James Feist8f2710a2018-05-09 17:18:55 -0700227 foundProbe = false;
228 return false;
James Feist3cb5fec2018-01-23 14:41:51 -0800229 }
230 foundProbe = true;
231
232 bool foundMatch = false;
James Feista465ccc2019-02-08 12:51:01 -0800233 for (auto& device : dbusObject)
James Feist3cb5fec2018-01-23 14:41:51 -0800234 {
235 bool deviceMatches = true;
James Feista465ccc2019-02-08 12:51:01 -0800236 for (auto& match : matches)
James Feist3cb5fec2018-01-23 14:41:51 -0800237 {
238 auto deviceValue = device.find(match.first);
239 if (deviceValue != device.end())
240 {
241 switch (match.second.type())
242 {
James Feist9eb0b582018-04-27 12:15:46 -0700243 case nlohmann::json::value_t::string:
James Feist3cb5fec2018-01-23 14:41:51 -0800244 {
James Feist9eb0b582018-04-27 12:15:46 -0700245 std::regex search(match.second.get<std::string>());
246 std::smatch match;
247
248 // convert value to string respresentation
James Feista465ccc2019-02-08 12:51:01 -0800249 std::string probeValue = std::visit(
James Feist8f2710a2018-05-09 17:18:55 -0700250 VariantToStringVisitor(), deviceValue->second);
James Feist9eb0b582018-04-27 12:15:46 -0700251 if (!std::regex_search(probeValue, match, search))
252 {
253 deviceMatches = false;
254 break;
255 }
James Feist3cb5fec2018-01-23 14:41:51 -0800256 break;
257 }
James Feist9eb0b582018-04-27 12:15:46 -0700258 case nlohmann::json::value_t::boolean:
259 case nlohmann::json::value_t::number_unsigned:
James Feist3cb5fec2018-01-23 14:41:51 -0800260 {
James Feista465ccc2019-02-08 12:51:01 -0800261 unsigned int probeValue = std::visit(
James Feist9eb0b582018-04-27 12:15:46 -0700262 VariantToUnsignedIntVisitor(), deviceValue->second);
James Feist3cb5fec2018-01-23 14:41:51 -0800263
James Feist9eb0b582018-04-27 12:15:46 -0700264 if (probeValue != match.second.get<unsigned int>())
265 {
266 deviceMatches = false;
267 }
268 break;
James Feist3cb5fec2018-01-23 14:41:51 -0800269 }
James Feist9eb0b582018-04-27 12:15:46 -0700270 case nlohmann::json::value_t::number_integer:
271 {
James Feista465ccc2019-02-08 12:51:01 -0800272 int probeValue = std::visit(VariantToIntVisitor(),
273 deviceValue->second);
James Feist3cb5fec2018-01-23 14:41:51 -0800274
James Feist9eb0b582018-04-27 12:15:46 -0700275 if (probeValue != match.second.get<int>())
276 {
277 deviceMatches = false;
278 }
279 break;
James Feist3cb5fec2018-01-23 14:41:51 -0800280 }
James Feist9eb0b582018-04-27 12:15:46 -0700281 case nlohmann::json::value_t::number_float:
282 {
James Feista465ccc2019-02-08 12:51:01 -0800283 float probeValue = std::visit(VariantToFloatVisitor(),
284 deviceValue->second);
James Feist9eb0b582018-04-27 12:15:46 -0700285
286 if (probeValue != match.second.get<float>())
287 {
288 deviceMatches = false;
289 }
290 break;
291 }
James Feist0eb40352019-04-09 14:44:04 -0700292 default:
293 {
294 std::cerr << "unexpected dbus probe type "
295 << match.second.type_name() << "\n";
296 }
James Feist3cb5fec2018-01-23 14:41:51 -0800297 }
298 }
299 else
300 {
301 deviceMatches = false;
302 break;
303 }
304 }
305 if (deviceMatches)
306 {
307 devices.emplace_back(
James Feist8f2710a2018-05-09 17:18:55 -0700308 boost::container::flat_map<std::string, BasicVariantType>(
James Feist3cb5fec2018-01-23 14:41:51 -0800309 device));
310 foundMatch = true;
311 deviceMatches = false; // for next iteration
312 }
313 }
314 return foundMatch;
315}
316
317// default probe entry point, iterates a list looking for specific types to
318// call specific probe functions
319bool probe(
James Feista465ccc2019-02-08 12:51:01 -0800320 const std::vector<std::string>& probeCommand,
James Feistf1b14142019-04-10 15:22:09 -0700321 std::vector<std::optional<
322 boost::container::flat_map<std::string, BasicVariantType>>>& foundDevs)
James Feist3cb5fec2018-01-23 14:41:51 -0800323{
324 const static std::regex command(R"(\((.*)\))");
325 std::smatch match;
326 bool ret = false;
James Feist6bd2a022018-03-13 12:30:58 -0700327 bool matchOne = false;
James Feist3cb5fec2018-01-23 14:41:51 -0800328 bool cur = true;
329 probe_type_codes lastCommand = probe_type_codes::FALSE_T;
330
James Feista465ccc2019-02-08 12:51:01 -0800331 for (auto& probe : probeCommand)
James Feist3cb5fec2018-01-23 14:41:51 -0800332 {
333 bool foundProbe = false;
James Feista465ccc2019-02-08 12:51:01 -0800334 boost::container::flat_map<const char*, probe_type_codes,
James Feist3cb5fec2018-01-23 14:41:51 -0800335 cmp_str>::const_iterator probeType;
336
337 for (probeType = PROBE_TYPES.begin(); probeType != PROBE_TYPES.end();
338 probeType++)
339 {
340 if (probe.find(probeType->first) != std::string::npos)
341 {
342 foundProbe = true;
343 break;
344 }
345 }
346 if (foundProbe)
347 {
348 switch (probeType->second)
349 {
James Feist9eb0b582018-04-27 12:15:46 -0700350 case probe_type_codes::FALSE_T:
James Feist3cb5fec2018-01-23 14:41:51 -0800351 {
James Feist8f2710a2018-05-09 17:18:55 -0700352 return false;
James Feist3cb5fec2018-01-23 14:41:51 -0800353 }
James Feist9eb0b582018-04-27 12:15:46 -0700354 case probe_type_codes::TRUE_T:
355 {
James Feist8f2710a2018-05-09 17:18:55 -0700356 return true;
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
426 switch (lastCommand)
427 {
James Feist9eb0b582018-04-27 12:15:46 -0700428 case probe_type_codes::AND:
429 ret = cur && ret;
430 break;
431 case probe_type_codes::OR:
432 ret = cur || ret;
433 break;
434 default:
435 ret = cur;
436 break;
James Feist3cb5fec2018-01-23 14:41:51 -0800437 }
438 lastCommand = probeType != PROBE_TYPES.end()
439 ? probeType->second
440 : probe_type_codes::FALSE_T;
James Feist3cb5fec2018-01-23 14:41:51 -0800441 }
442
443 // probe passed, but empty device
James Feist3cb5fec2018-01-23 14:41:51 -0800444 if (ret && foundDevs.size() == 0)
445 {
James Feistf1b14142019-04-10 15:22:09 -0700446 foundDevs.emplace_back(std::nullopt);
James Feist3cb5fec2018-01-23 14:41:51 -0800447 }
James Feist0eb40352019-04-09 14:44:04 -0700448 if (matchOne && ret)
James Feist6bd2a022018-03-13 12:30:58 -0700449 {
James Feist0eb40352019-04-09 14:44:04 -0700450 // match one could match multiple dbus values, which means we don't care
451 // what one we found so we shouldn't be using template replace. return
452 // an empty one
453 foundDevs.clear();
James Feistf1b14142019-04-10 15:22:09 -0700454 foundDevs.emplace_back(std::nullopt);
James Feist6bd2a022018-03-13 12:30:58 -0700455 }
James Feist3cb5fec2018-01-23 14:41:51 -0800456 return ret;
457}
James Feist8f2710a2018-05-09 17:18:55 -0700458// this class finds the needed dbus fields and on destruction runs the probe
459struct PerformProbe : std::enable_shared_from_this<PerformProbe>
460{
James Feist3cb5fec2018-01-23 14:41:51 -0800461
James Feist8f2710a2018-05-09 17:18:55 -0700462 PerformProbe(
James Feista465ccc2019-02-08 12:51:01 -0800463 const std::vector<std::string>& probeCommand,
James Feistf1b14142019-04-10 15:22:09 -0700464 std::function<void(std::vector<std::optional<boost::container::flat_map<
465 std::string, BasicVariantType>>>&)>&& callback) :
James Feist8f2710a2018-05-09 17:18:55 -0700466 _probeCommand(probeCommand),
467 _callback(std::move(callback))
468 {
469 }
470 ~PerformProbe()
471 {
James Feistf1b14142019-04-10 15:22:09 -0700472 std::vector<std::optional<
473 boost::container::flat_map<std::string, BasicVariantType>>>
James Feist0eb40352019-04-09 14:44:04 -0700474 foundDevs;
475 if (probe(_probeCommand, foundDevs))
James Feist8f2710a2018-05-09 17:18:55 -0700476 {
James Feist0eb40352019-04-09 14:44:04 -0700477 _callback(foundDevs);
James Feist8f2710a2018-05-09 17:18:55 -0700478 }
479 }
480 void run()
481 {
482 // parse out dbus probes by discarding other probe types
James Feista465ccc2019-02-08 12:51:01 -0800483 boost::container::flat_map<const char*, probe_type_codes,
James Feist8f2710a2018-05-09 17:18:55 -0700484 cmp_str>::const_iterator probeType;
485
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 }
542 catch (const std::out_of_range)
543 {
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)}]() {
638 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
639 interface.lock();
640 if (!iface)
641 {
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);
647 if (!objServer.remove_interface(iface))
648 {
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
914 if (!writeJsonFiles(systemConfiguration))
915 {
916 std::cerr << "Error writing json files\n";
917 throw DBusInternalError();
918 }
919 std::string dbusName = *name;
920
921 std::regex_replace(dbusName.begin(), dbusName.begin(),
Johnathan Mantey2015f752019-03-26 15:22:31 -0700922 dbusName.end(), ILLEGAL_DBUS_MEMBER_REGEX, "_");
James Feist68500ff2018-08-08 15:40:42 -0700923 auto iface = objServer.add_interface(
924 path + "/" + dbusName,
925 "xyz.openbmc_project.Configuration." + *type);
926 // permission is read-write, as since we just created it, must be
927 // runtime modifiable
928 populateInterfaceFromJson(
929 systemConfiguration,
James Feistc6248a52018-08-14 10:09:45 -0700930 jsonPointerPath + "/" + std::to_string(lastIndex), iface,
James Feist68500ff2018-08-08 15:40:42 -0700931 newData, objServer,
932 sdbusplus::asio::PropertyPermission::readWrite);
933 // todo(james) generate patch
934 findExposes->push_back(newData);
935 });
936 iface->initialize();
937}
938
James Feista465ccc2019-02-08 12:51:01 -0800939void postToDbus(const nlohmann::json& newConfiguration,
940 nlohmann::json& systemConfiguration,
941 sdbusplus::asio::object_server& objServer)
James Feist75fdeeb2018-02-20 14:26:16 -0800942
James Feist1b2e2242018-01-30 13:45:19 -0800943{
James Feist97a63f12018-05-17 13:50:57 -0700944 // iterate through boards
James Feista465ccc2019-02-08 12:51:01 -0800945 for (auto& boardPair : newConfiguration.items())
James Feist1b2e2242018-01-30 13:45:19 -0800946 {
James Feistf1b14142019-04-10 15:22:09 -0700947 std::string boardKey = boardPair.value()["Name"];
948 std::string jsonPointerPath = "/" + boardPair.key();
James Feist97a63f12018-05-17 13:50:57 -0700949 // loop through newConfiguration, but use values from system
950 // configuration to be able to modify via dbus later
James Feistf1b14142019-04-10 15:22:09 -0700951 auto boardValues = systemConfiguration[boardPair.key()];
James Feistd63d18a2018-07-19 15:23:45 -0700952 auto findBoardType = boardValues.find("Type");
James Feist1b2e2242018-01-30 13:45:19 -0800953 std::string boardType;
954 if (findBoardType != boardValues.end() &&
955 findBoardType->type() == nlohmann::json::value_t::string)
956 {
957 boardType = findBoardType->get<std::string>();
958 std::regex_replace(boardType.begin(), boardType.begin(),
Johnathan Mantey2015f752019-03-26 15:22:31 -0700959 boardType.end(), ILLEGAL_DBUS_MEMBER_REGEX, "_");
James Feist1b2e2242018-01-30 13:45:19 -0800960 }
961 else
962 {
963 std::cerr << "Unable to find type for " << boardKey
964 << " reverting to Chassis.\n";
965 boardType = "Chassis";
966 }
James Feist11be6672018-04-06 14:05:32 -0700967 std::string boardtypeLower = boost::algorithm::to_lower_copy(boardType);
James Feist1b2e2242018-01-30 13:45:19 -0800968
969 std::regex_replace(boardKey.begin(), boardKey.begin(), boardKey.end(),
Johnathan Mantey2015f752019-03-26 15:22:31 -0700970 ILLEGAL_DBUS_MEMBER_REGEX, "_");
James Feist11be6672018-04-06 14:05:32 -0700971 std::string boardName = "/xyz/openbmc_project/inventory/system/" +
972 boardtypeLower + "/" + boardKey;
James Feist1b2e2242018-01-30 13:45:19 -0800973
James Feist8f2710a2018-05-09 17:18:55 -0700974 auto inventoryIface = objServer.add_interface(
975 boardName, "xyz.openbmc_project.Inventory.Item");
James Feist68500ff2018-08-08 15:40:42 -0700976
James Feist8f2710a2018-05-09 17:18:55 -0700977 auto boardIface = objServer.add_interface(
978 boardName, "xyz.openbmc_project.Inventory.Item." + boardType);
James Feist11be6672018-04-06 14:05:32 -0700979
James Feist68500ff2018-08-08 15:40:42 -0700980 createAddObjectMethod(jsonPointerPath, boardName, systemConfiguration,
981 objServer);
982
James Feist97a63f12018-05-17 13:50:57 -0700983 populateInterfaceFromJson(systemConfiguration, jsonPointerPath,
James Feistc6248a52018-08-14 10:09:45 -0700984 boardIface, boardValues, objServer);
James Feist97a63f12018-05-17 13:50:57 -0700985 jsonPointerPath += "/";
986 // iterate through board properties
James Feista465ccc2019-02-08 12:51:01 -0800987 for (auto& boardField : boardValues.items())
James Feist11be6672018-04-06 14:05:32 -0700988 {
989 if (boardField.value().type() == nlohmann::json::value_t::object)
990 {
James Feist8f2710a2018-05-09 17:18:55 -0700991 auto iface =
992 objServer.add_interface(boardName, boardField.key());
James Feistc6248a52018-08-14 10:09:45 -0700993 populateInterfaceFromJson(systemConfiguration,
994 jsonPointerPath + boardField.key(),
995 iface, boardField.value(), objServer);
James Feist11be6672018-04-06 14:05:32 -0700996 }
997 }
James Feist97a63f12018-05-17 13:50:57 -0700998
James Feist1e3e6982018-08-03 16:09:28 -0700999 auto exposes = boardValues.find("Exposes");
James Feist1b2e2242018-01-30 13:45:19 -08001000 if (exposes == boardValues.end())
1001 {
1002 continue;
1003 }
James Feist97a63f12018-05-17 13:50:57 -07001004 // iterate through exposes
James Feist1e3e6982018-08-03 16:09:28 -07001005 jsonPointerPath += "Exposes/";
James Feist97a63f12018-05-17 13:50:57 -07001006
1007 // store the board level pointer so we can modify it on the way down
1008 std::string jsonPointerPathBoard = jsonPointerPath;
1009 size_t exposesIndex = -1;
James Feista465ccc2019-02-08 12:51:01 -08001010 for (auto& item : *exposes)
James Feist1b2e2242018-01-30 13:45:19 -08001011 {
James Feist97a63f12018-05-17 13:50:57 -07001012 exposesIndex++;
1013 jsonPointerPath = jsonPointerPathBoard;
1014 jsonPointerPath += std::to_string(exposesIndex);
1015
James Feistd63d18a2018-07-19 15:23:45 -07001016 auto findName = item.find("Name");
James Feist1b2e2242018-01-30 13:45:19 -08001017 if (findName == item.end())
1018 {
1019 std::cerr << "cannot find name in field " << item << "\n";
1020 continue;
1021 }
James Feist1e3e6982018-08-03 16:09:28 -07001022 auto findStatus = item.find("Status");
James Feist1b2e2242018-01-30 13:45:19 -08001023 // if status is not found it is assumed to be status = 'okay'
1024 if (findStatus != item.end())
1025 {
1026 if (*findStatus == "disabled")
1027 {
1028 continue;
1029 }
1030 }
James Feistd63d18a2018-07-19 15:23:45 -07001031 auto findType = item.find("Type");
James Feist1b2e2242018-01-30 13:45:19 -08001032 std::string itemType;
1033 if (findType != item.end())
1034 {
1035 itemType = findType->get<std::string>();
1036 std::regex_replace(itemType.begin(), itemType.begin(),
Johnathan Mantey2015f752019-03-26 15:22:31 -07001037 itemType.end(), ILLEGAL_DBUS_PATH_REGEX,
1038 "_");
James Feist1b2e2242018-01-30 13:45:19 -08001039 }
1040 else
1041 {
1042 itemType = "unknown";
1043 }
1044 std::string itemName = findName->get<std::string>();
1045 std::regex_replace(itemName.begin(), itemName.begin(),
Johnathan Mantey2015f752019-03-26 15:22:31 -07001046 itemName.end(), ILLEGAL_DBUS_MEMBER_REGEX, "_");
James Feistc6248a52018-08-14 10:09:45 -07001047
James Feist8f2710a2018-05-09 17:18:55 -07001048 auto itemIface = objServer.add_interface(
1049 boardName + "/" + itemName,
James Feist1b2e2242018-01-30 13:45:19 -08001050 "xyz.openbmc_project.Configuration." + itemType);
1051
James Feist97a63f12018-05-17 13:50:57 -07001052 populateInterfaceFromJson(systemConfiguration, jsonPointerPath,
James Feistc6248a52018-08-14 10:09:45 -07001053 itemIface, item, objServer,
1054 getPermission(itemType));
James Feist1b2e2242018-01-30 13:45:19 -08001055
James Feista465ccc2019-02-08 12:51:01 -08001056 for (auto& objectPair : item.items())
James Feist1b2e2242018-01-30 13:45:19 -08001057 {
James Feist97a63f12018-05-17 13:50:57 -07001058 jsonPointerPath = jsonPointerPathBoard +
1059 std::to_string(exposesIndex) + "/" +
1060 objectPair.key();
James Feist1b2e2242018-01-30 13:45:19 -08001061 if (objectPair.value().type() ==
1062 nlohmann::json::value_t::object)
1063 {
James Feist8f2710a2018-05-09 17:18:55 -07001064 auto objectIface = objServer.add_interface(
1065 boardName + "/" + itemName,
James Feist1b2e2242018-01-30 13:45:19 -08001066 "xyz.openbmc_project.Configuration." + itemType + "." +
James Feist8f2710a2018-05-09 17:18:55 -07001067 objectPair.key());
James Feist97a63f12018-05-17 13:50:57 -07001068
1069 populateInterfaceFromJson(
James Feistc6248a52018-08-14 10:09:45 -07001070 systemConfiguration, jsonPointerPath, objectIface,
1071 objectPair.value(), objServer, getPermission(itemType));
James Feist1b2e2242018-01-30 13:45:19 -08001072 }
1073 else if (objectPair.value().type() ==
1074 nlohmann::json::value_t::array)
1075 {
1076 size_t index = 0;
James Feist8f2710a2018-05-09 17:18:55 -07001077 if (!objectPair.value().size())
James Feist1b2e2242018-01-30 13:45:19 -08001078 {
James Feist8f2710a2018-05-09 17:18:55 -07001079 continue;
1080 }
1081 bool isLegal = true;
1082 auto type = objectPair.value()[0].type();
1083 if (type != nlohmann::json::value_t::object)
1084 {
1085 continue;
1086 }
1087
1088 // verify legal json
James Feista465ccc2019-02-08 12:51:01 -08001089 for (const auto& arrayItem : objectPair.value())
James Feist8f2710a2018-05-09 17:18:55 -07001090 {
1091 if (arrayItem.type() != type)
James Feist1b2e2242018-01-30 13:45:19 -08001092 {
James Feist8f2710a2018-05-09 17:18:55 -07001093 isLegal = false;
James Feist1b2e2242018-01-30 13:45:19 -08001094 break;
1095 }
James Feist8f2710a2018-05-09 17:18:55 -07001096 }
1097 if (!isLegal)
1098 {
1099 std::cerr << "dbus format error" << objectPair.value()
1100 << "\n";
1101 break;
1102 }
1103
James Feista465ccc2019-02-08 12:51:01 -08001104 for (auto& arrayItem : objectPair.value())
James Feist8f2710a2018-05-09 17:18:55 -07001105 {
James Feist97a63f12018-05-17 13:50:57 -07001106
James Feist8f2710a2018-05-09 17:18:55 -07001107 auto objectIface = objServer.add_interface(
1108 boardName + "/" + itemName,
James Feist1b2e2242018-01-30 13:45:19 -08001109 "xyz.openbmc_project.Configuration." + itemType +
James Feistbb43d022018-06-12 15:44:33 -07001110 "." + objectPair.key() + std::to_string(index));
James Feistc6248a52018-08-14 10:09:45 -07001111 populateInterfaceFromJson(
1112 systemConfiguration,
1113 jsonPointerPath + "/" + std::to_string(index),
1114 objectIface, arrayItem, objServer,
1115 getPermission(objectPair.key()));
James Feistbb43d022018-06-12 15:44:33 -07001116 index++;
James Feist1b2e2242018-01-30 13:45:19 -08001117 }
1118 }
1119 }
1120 }
1121 }
1122}
1123
1124// finds the template character (currently set to $) and replaces the value with
1125// the field found in a dbus object i.e. $ADDRESS would get populated with the
1126// ADDRESS field from a object on dbus
1127void templateCharReplace(
James Feista465ccc2019-02-08 12:51:01 -08001128 nlohmann::json::iterator& keyPair,
1129 const boost::container::flat_map<std::string, BasicVariantType>&
1130 foundDevice,
1131 size_t& foundDeviceIdx)
James Feist1b2e2242018-01-30 13:45:19 -08001132{
James Feist11be6672018-04-06 14:05:32 -07001133 if (keyPair.value().type() == nlohmann::json::value_t::object)
1134 {
1135 for (auto nextLayer = keyPair.value().begin();
1136 nextLayer != keyPair.value().end(); nextLayer++)
1137 {
1138 templateCharReplace(nextLayer, foundDevice, foundDeviceIdx);
1139 }
1140 return;
1141 }
Ed Tanous12bc7932019-02-26 14:36:20 -08001142
1143 std::string* strPtr = keyPair.value().get_ptr<std::string*>();
1144 if (strPtr == nullptr)
James Feist1b2e2242018-01-30 13:45:19 -08001145 {
1146 return;
1147 }
1148
Ed Tanous12bc7932019-02-26 14:36:20 -08001149 boost::replace_all(*strPtr, "$index", std::to_string(foundDeviceIdx));
1150
Ed Tanous12bc7932019-02-26 14:36:20 -08001151 for (auto& foundDevicePair : foundDevice)
James Feist1b2e2242018-01-30 13:45:19 -08001152 {
Ed Tanous12bc7932019-02-26 14:36:20 -08001153 std::string templateName = "$" + foundDevicePair.first;
1154 if (boost::iequals(*strPtr, templateName))
James Feist1b2e2242018-01-30 13:45:19 -08001155 {
Ed Tanous12bc7932019-02-26 14:36:20 -08001156 std::visit([&](auto&& val) { keyPair.value() = val; },
1157 foundDevicePair.second);
1158 // We probably just invalidated the pointer above, so set it to null
1159 strPtr = nullptr;
1160 break;
James Feist1b2e2242018-01-30 13:45:19 -08001161 }
Ed Tanous12bc7932019-02-26 14:36:20 -08001162
1163 std::string probeValue =
1164 std::visit(VariantToStringVisitor(), foundDevicePair.second);
1165 boost::replace_all(*strPtr, templateName, probeValue);
1166 }
1167
1168 strPtr = keyPair.value().get_ptr<std::string*>();
1169 if (strPtr == nullptr)
1170 {
1171 return;
James Feist1b2e2242018-01-30 13:45:19 -08001172 }
James Feistc6090822019-01-04 16:02:48 -08001173
1174 // convert hex numbers to ints
Ed Tanous12bc7932019-02-26 14:36:20 -08001175 if (boost::starts_with(*strPtr, "0x"))
James Feist28dc2da2018-10-15 14:47:42 -07001176 {
1177 try
1178 {
James Feistc6090822019-01-04 16:02:48 -08001179 size_t pos = 0;
Ed Tanous12bc7932019-02-26 14:36:20 -08001180 int64_t temp = std::stoul(*strPtr, &pos, 0);
1181 if (pos == strPtr->size())
James Feistc6090822019-01-04 16:02:48 -08001182 {
1183 keyPair.value() = static_cast<uint64_t>(temp);
1184 }
James Feist28dc2da2018-10-15 14:47:42 -07001185 }
1186 catch (std::invalid_argument)
1187 {
1188 }
James Feistc6090822019-01-04 16:02:48 -08001189 catch (std::out_of_range)
1190 {
1191 }
James Feist28dc2da2018-10-15 14:47:42 -07001192 }
James Feist1b2e2242018-01-30 13:45:19 -08001193}
1194
James Feist8f2710a2018-05-09 17:18:55 -07001195// reads json files out of the filesystem
James Feista465ccc2019-02-08 12:51:01 -08001196bool findJsonFiles(std::list<nlohmann::json>& configurations)
James Feist3cb5fec2018-01-23 14:41:51 -08001197{
1198 // find configuration files
Ed Tanous072e25d2018-12-16 21:45:20 -08001199 std::vector<std::filesystem::path> jsonPaths;
Johnathan Mantey2015f752019-03-26 15:22:31 -07001200 if (!findFiles(std::filesystem::path(configurationDirectory), R"(.*\.json)",
1201 jsonPaths))
James Feist3cb5fec2018-01-23 14:41:51 -08001202 {
1203 std::cerr << "Unable to find any configuration files in "
James Feistb4383f42018-08-06 16:54:10 -07001204 << configurationDirectory << "\n";
James Feist75fdeeb2018-02-20 14:26:16 -08001205 return false;
James Feist3cb5fec2018-01-23 14:41:51 -08001206 }
James Feistb4383f42018-08-06 16:54:10 -07001207
1208 std::ifstream schemaStream(std::string(schemaDirectory) + "/" +
1209 globalSchema);
1210 if (!schemaStream.good())
1211 {
1212 std::cerr
1213 << "Cannot open schema file, cannot validate JSON, exiting\n\n";
1214 std::exit(EXIT_FAILURE);
Ed Tanous072e25d2018-12-16 21:45:20 -08001215 return false;
James Feistb4383f42018-08-06 16:54:10 -07001216 }
1217 nlohmann::json schema = nlohmann::json::parse(schemaStream, nullptr, false);
1218 if (schema.is_discarded())
1219 {
1220 std::cerr
1221 << "Illegal schema file detected, cannot validate JSON, exiting\n";
1222 std::exit(EXIT_FAILURE);
Ed Tanous072e25d2018-12-16 21:45:20 -08001223 return false;
James Feistb4383f42018-08-06 16:54:10 -07001224 }
1225
James Feista465ccc2019-02-08 12:51:01 -08001226 for (auto& jsonPath : jsonPaths)
James Feist3cb5fec2018-01-23 14:41:51 -08001227 {
1228 std::ifstream jsonStream(jsonPath.c_str());
1229 if (!jsonStream.good())
1230 {
1231 std::cerr << "unable to open " << jsonPath.string() << "\n";
1232 continue;
1233 }
1234 auto data = nlohmann::json::parse(jsonStream, nullptr, false);
1235 if (data.is_discarded())
1236 {
1237 std::cerr << "syntax error in " << jsonPath.string() << "\n";
1238 continue;
1239 }
James Feist8da99192019-01-24 08:20:16 -08001240 /*
1241 * todo(james): reenable this once less things are in flight
1242 *
James Feistb4383f42018-08-06 16:54:10 -07001243 if (!validateJson(schema, data))
1244 {
1245 std::cerr << "Error validating " << jsonPath.string() << "\n";
1246 continue;
1247 }
James Feist8da99192019-01-24 08:20:16 -08001248 */
James Feistb4383f42018-08-06 16:54:10 -07001249
James Feist3cb5fec2018-01-23 14:41:51 -08001250 if (data.type() == nlohmann::json::value_t::array)
1251 {
James Feista465ccc2019-02-08 12:51:01 -08001252 for (auto& d : data)
James Feist3cb5fec2018-01-23 14:41:51 -08001253 {
1254 configurations.emplace_back(d);
1255 }
1256 }
1257 else
1258 {
1259 configurations.emplace_back(data);
1260 }
1261 }
Ed Tanous072e25d2018-12-16 21:45:20 -08001262 return true;
James Feist75fdeeb2018-02-20 14:26:16 -08001263}
James Feist3cb5fec2018-01-23 14:41:51 -08001264
James Feist8f2710a2018-05-09 17:18:55 -07001265struct PerformScan : std::enable_shared_from_this<PerformScan>
James Feist75fdeeb2018-02-20 14:26:16 -08001266{
James Feist75fdeeb2018-02-20 14:26:16 -08001267
James Feista465ccc2019-02-08 12:51:01 -08001268 PerformScan(nlohmann::json& systemConfiguration,
1269 std::list<nlohmann::json>& configurations,
1270 std::function<void(void)>&& callback) :
James Feist8f2710a2018-05-09 17:18:55 -07001271 _systemConfiguration(systemConfiguration),
1272 _configurations(configurations), _callback(std::move(callback))
James Feist3cb5fec2018-01-23 14:41:51 -08001273 {
James Feist8f2710a2018-05-09 17:18:55 -07001274 }
1275 void run()
1276 {
1277 for (auto it = _configurations.begin(); it != _configurations.end();)
James Feist3cb5fec2018-01-23 14:41:51 -08001278 {
James Feist1e3e6982018-08-03 16:09:28 -07001279 auto findProbe = it->find("Probe");
James Feistd63d18a2018-07-19 15:23:45 -07001280 auto findName = it->find("Name");
James Feist3cb5fec2018-01-23 14:41:51 -08001281
James Feist1b2e2242018-01-30 13:45:19 -08001282 nlohmann::json probeCommand;
1283 // check for poorly formatted fields, probe must be an array
1284 if (findProbe == it->end())
James Feist3cb5fec2018-01-23 14:41:51 -08001285 {
1286 std::cerr << "configuration file missing probe:\n " << *it
1287 << "\n";
James Feist8f2710a2018-05-09 17:18:55 -07001288 it = _configurations.erase(it);
1289 continue;
James Feist3cb5fec2018-01-23 14:41:51 -08001290 }
James Feist1b2e2242018-01-30 13:45:19 -08001291 else if ((*findProbe).type() != nlohmann::json::value_t::array)
James Feist3cb5fec2018-01-23 14:41:51 -08001292 {
1293 probeCommand = nlohmann::json::array();
1294 probeCommand.push_back(*findProbe);
1295 }
1296 else
1297 {
1298 probeCommand = *findProbe;
1299 }
James Feist1b2e2242018-01-30 13:45:19 -08001300
1301 if (findName == it->end())
1302 {
1303 std::cerr << "configuration file missing name:\n " << *it
1304 << "\n";
James Feist8f2710a2018-05-09 17:18:55 -07001305 it = _configurations.erase(it);
1306 continue;
James Feist1b2e2242018-01-30 13:45:19 -08001307 }
James Feistf1b14142019-04-10 15:22:09 -07001308 std::string probeName = *findName;
James Feist1b2e2242018-01-30 13:45:19 -08001309
James Feistf1b14142019-04-10 15:22:09 -07001310 if (std::find(PASSED_PROBES.begin(), PASSED_PROBES.end(),
1311 probeName) != PASSED_PROBES.end())
James Feist3cb5fec2018-01-23 14:41:51 -08001312 {
James Feist8f2710a2018-05-09 17:18:55 -07001313 it = _configurations.erase(it);
1314 continue;
1315 }
James Feistf1b14142019-04-10 15:22:09 -07001316 nlohmann::json* recordPtr = &(*it);
James Feist3cb5fec2018-01-23 14:41:51 -08001317
James Feist8f2710a2018-05-09 17:18:55 -07001318 // store reference to this to children to makes sure we don't get
1319 // destroyed too early
1320 auto thisRef = shared_from_this();
1321 auto p = std::make_shared<PerformProbe>(
1322 probeCommand,
James Feistf1b14142019-04-10 15:22:09 -07001323 [&, recordPtr, probeName,
1324 thisRef](std::vector<std::optional<boost::container::flat_map<
1325 std::string, BasicVariantType>>>& foundDevices) {
James Feist8f2710a2018-05-09 17:18:55 -07001326 _passed = true;
James Feist3cb5fec2018-01-23 14:41:51 -08001327
James Feistf1b14142019-04-10 15:22:09 -07001328 PASSED_PROBES.push_back(probeName);
James Feist8f2710a2018-05-09 17:18:55 -07001329 size_t foundDeviceIdx = 0;
1330
James Feistf1b14142019-04-10 15:22:09 -07001331 nlohmann::json record = *recordPtr;
James Feistbe5425f2018-06-08 10:30:55 -07001332
James Feista465ccc2019-02-08 12:51:01 -08001333 for (auto& foundDevice : foundDevices)
James Feist3cb5fec2018-01-23 14:41:51 -08001334 {
James Feistf1b14142019-04-10 15:22:09 -07001335 std::string recordName;
1336 size_t hash = 0;
1337 if (foundDevice)
James Feist3cb5fec2018-01-23 14:41:51 -08001338 {
James Feistf1b14142019-04-10 15:22:09 -07001339 // use an array so alphabetical order from the
1340 // flat_map is maintained
1341 auto device = nlohmann::json::array();
1342 for (auto& devPair : *foundDevice)
1343 {
1344 device.push_back(devPair.first);
1345 std::visit(
1346 [&device](auto&& v) {
1347 device.push_back(v);
1348 },
1349 devPair.second);
1350 }
1351 hash = std::hash<std::string>{}(probeName +
1352 device.dump());
1353 // hashes are hard to distinguish, use the
1354 // non-hashed version if we want debug
1355 if constexpr (DEBUG)
1356 {
1357 recordName = probeName + device.dump();
1358 }
1359 else
1360 {
1361 recordName = std::to_string(hash);
1362 }
James Feist8f2710a2018-05-09 17:18:55 -07001363 }
James Feistf1b14142019-04-10 15:22:09 -07001364 else
1365 {
1366 recordName = probeName;
1367 }
1368
James Feist1df06a42019-04-11 14:23:04 -07001369 auto fromLastJson = lastJson.find(recordName);
1370 if (fromLastJson != lastJson.end())
1371 {
1372 // keep user changes
1373 _systemConfiguration[recordName] = *fromLastJson;
1374 continue;
1375 }
1376
James Feistf1b14142019-04-10 15:22:09 -07001377 // insert into configuration temporarily to be able to
1378 // reference ourselves
1379
1380 _systemConfiguration[recordName] = record;
1381
1382 if (foundDevice)
1383 {
1384 for (auto keyPair = record.begin();
1385 keyPair != record.end(); keyPair++)
1386 {
1387 templateCharReplace(keyPair, *foundDevice,
1388 foundDeviceIdx);
1389 }
1390 }
1391 auto findExpose = record.find("Exposes");
1392 if (findExpose == record.end())
James Feist8f2710a2018-05-09 17:18:55 -07001393 {
1394 continue;
1395 }
James Feista465ccc2019-02-08 12:51:01 -08001396 for (auto& expose : *findExpose)
James Feist8f2710a2018-05-09 17:18:55 -07001397 {
1398 for (auto keyPair = expose.begin();
1399 keyPair != expose.end(); keyPair++)
James Feist3cb5fec2018-01-23 14:41:51 -08001400 {
James Feist1b2e2242018-01-30 13:45:19 -08001401
James Feist8f2710a2018-05-09 17:18:55 -07001402 // fill in template characters with devices
1403 // found
James Feistf1b14142019-04-10 15:22:09 -07001404 if (foundDevice)
1405 {
1406 templateCharReplace(keyPair, *foundDevice,
1407 foundDeviceIdx);
1408 }
James Feist8f2710a2018-05-09 17:18:55 -07001409 // special case bind
James Feist1e3e6982018-08-03 16:09:28 -07001410 if (boost::starts_with(keyPair.key(), "Bind"))
James Feist8f2710a2018-05-09 17:18:55 -07001411 {
1412 if (keyPair.value().type() !=
1413 nlohmann::json::value_t::string)
James Feist3cb5fec2018-01-23 14:41:51 -08001414 {
James Feist8f2710a2018-05-09 17:18:55 -07001415 std::cerr << "bind_ value must be of "
1416 "type string "
1417 << keyPair.key() << "\n";
James Feist1b2e2242018-01-30 13:45:19 -08001418 continue;
1419 }
James Feist8f2710a2018-05-09 17:18:55 -07001420 bool foundBind = false;
1421 std::string bind = keyPair.key().substr(
James Feist1e3e6982018-08-03 16:09:28 -07001422 sizeof("Bind") - 1);
James Feistbe5425f2018-06-08 10:30:55 -07001423
James Feista465ccc2019-02-08 12:51:01 -08001424 for (auto& configurationPair :
James Feist8f2710a2018-05-09 17:18:55 -07001425 _systemConfiguration.items())
James Feist1b2e2242018-01-30 13:45:19 -08001426 {
James Feist1b2e2242018-01-30 13:45:19 -08001427
James Feist8f2710a2018-05-09 17:18:55 -07001428 auto configListFind =
1429 configurationPair.value().find(
James Feist1e3e6982018-08-03 16:09:28 -07001430 "Exposes");
James Feist8f2710a2018-05-09 17:18:55 -07001431
1432 if (configListFind ==
1433 configurationPair.value()
1434 .end() ||
1435 configListFind->type() !=
1436 nlohmann::json::value_t::array)
1437 {
1438 continue;
1439 }
James Feista465ccc2019-02-08 12:51:01 -08001440 for (auto& exposedObject :
James Feist8f2710a2018-05-09 17:18:55 -07001441 *configListFind)
1442 {
1443 std::string foundObjectName =
James Feistd63d18a2018-07-19 15:23:45 -07001444 (exposedObject)["Name"];
James Feist8f2710a2018-05-09 17:18:55 -07001445 if (boost::iequals(
1446 foundObjectName,
1447 keyPair.value()
1448 .get<std::string>()))
1449 {
James Feist1e3e6982018-08-03 16:09:28 -07001450 exposedObject["Status"] =
James Feist8f2710a2018-05-09 17:18:55 -07001451 "okay";
1452 expose[bind] = exposedObject;
1453
1454 foundBind = true;
1455 break;
1456 }
1457 }
1458 if (foundBind)
1459 {
James Feist3cb5fec2018-01-23 14:41:51 -08001460 break;
1461 }
1462 }
James Feist8f2710a2018-05-09 17:18:55 -07001463 if (!foundBind)
James Feist3cb5fec2018-01-23 14:41:51 -08001464 {
James Feist8f2710a2018-05-09 17:18:55 -07001465 std::cerr << "configuration file "
1466 "dependency error, "
1467 "could not find bind "
1468 << keyPair.value() << "\n";
James Feist3cb5fec2018-01-23 14:41:51 -08001469 }
1470 }
1471 }
1472 }
James Feistf1b14142019-04-10 15:22:09 -07001473 // overwrite ourselves with cleaned up version
1474 _systemConfiguration[recordName] = record;
James Feist1df06a42019-04-11 14:23:04 -07001475 logDeviceAdded(record.at("Name").get<std::string>());
1476
James Feistf1b14142019-04-10 15:22:09 -07001477 foundDeviceIdx++;
James Feist3cb5fec2018-01-23 14:41:51 -08001478 }
James Feist8f2710a2018-05-09 17:18:55 -07001479 });
1480 p->run();
1481 it++;
James Feist3cb5fec2018-01-23 14:41:51 -08001482 }
1483 }
James Feist75fdeeb2018-02-20 14:26:16 -08001484
James Feist8f2710a2018-05-09 17:18:55 -07001485 ~PerformScan()
James Feist75fdeeb2018-02-20 14:26:16 -08001486 {
James Feist8f2710a2018-05-09 17:18:55 -07001487 if (_passed)
1488 {
1489 auto nextScan = std::make_shared<PerformScan>(
1490 _systemConfiguration, _configurations, std::move(_callback));
1491 nextScan->run();
1492 }
1493 else
1494 {
1495 _callback();
1496 }
1497 }
James Feista465ccc2019-02-08 12:51:01 -08001498 nlohmann::json& _systemConfiguration;
James Feist8f2710a2018-05-09 17:18:55 -07001499 std::list<nlohmann::json> _configurations;
1500 std::function<void(void)> _callback;
1501 std::vector<std::shared_ptr<PerformProbe>> _probes;
1502 bool _passed = false;
James Feist1df06a42019-04-11 14:23:04 -07001503 bool powerWasOn = isPowerOn();
James Feist8f2710a2018-05-09 17:18:55 -07001504};
James Feistc95cb142018-02-26 10:41:42 -08001505
James Feist1df06a42019-04-11 14:23:04 -07001506void startRemovedTimer(boost::asio::deadline_timer& timer,
1507 std::vector<sdbusplus::bus::match::match>& dbusMatches,
1508 nlohmann::json& systemConfiguration)
1509{
1510 static bool scannedPowerOff = false;
1511 static bool scannedPowerOn = false;
1512
1513 if (scannedPowerOn)
1514 {
1515 return;
1516 }
1517
1518 if (!isPowerOn() && scannedPowerOff)
1519 {
1520 return;
1521 }
1522
1523 timer.expires_from_now(boost::posix_time::seconds(10));
1524 timer.async_wait([&systemConfiguration](
1525 const boost::system::error_code& ec) {
1526 if (ec == boost::asio::error::operation_aborted)
1527 {
1528 // we were cancelled
1529 return;
1530 }
1531
1532 bool powerOff = !isPowerOn();
1533 for (const auto& item : lastJson.items())
1534 {
1535 if (systemConfiguration.find(item.key()) ==
1536 systemConfiguration.end())
1537 {
1538 bool isDetectedPowerOn = false;
1539 auto powerState = item.value().find("PowerState");
1540 if (powerState != item.value().end())
1541 {
1542 auto ptr = powerState->get_ptr<const std::string*>();
1543 if (ptr)
1544 {
1545 if (*ptr == "On" || *ptr == "BiosPost")
1546 {
1547 isDetectedPowerOn = true;
1548 }
1549 }
1550 }
1551 if (powerOff && isDetectedPowerOn)
1552 {
1553 // power not on yet, don't know if it's there or not
1554 continue;
1555 }
1556 if (!powerOff && scannedPowerOff && isDetectedPowerOn)
1557 {
1558 // already logged it when power was off
1559 continue;
1560 }
1561
1562 logDeviceRemoved(item.value().at("Name").get<std::string>());
1563 }
1564 }
1565 scannedPowerOff = true;
1566 if (!powerOff)
1567 {
1568 scannedPowerOn = true;
1569 }
1570 });
1571}
1572
James Feist8f2710a2018-05-09 17:18:55 -07001573// main properties changed entry
1574void propertiesChangedCallback(
James Feista465ccc2019-02-08 12:51:01 -08001575 boost::asio::io_service& io,
1576 std::vector<sdbusplus::bus::match::match>& dbusMatches,
1577 nlohmann::json& systemConfiguration,
1578 sdbusplus::asio::object_server& objServer)
James Feist8f2710a2018-05-09 17:18:55 -07001579{
1580 static boost::asio::deadline_timer timer(io);
James Feist1df06a42019-04-11 14:23:04 -07001581 static bool timerRunning;
1582
1583 timerRunning = true;
James Feist8f2710a2018-05-09 17:18:55 -07001584 timer.expires_from_now(boost::posix_time::seconds(1));
1585
1586 // setup an async wait as we normally get flooded with new requests
James Feista465ccc2019-02-08 12:51:01 -08001587 timer.async_wait([&](const boost::system::error_code& ec) {
James Feist8f2710a2018-05-09 17:18:55 -07001588 if (ec == boost::asio::error::operation_aborted)
1589 {
1590 // we were cancelled
1591 return;
1592 }
1593 else if (ec)
1594 {
1595 std::cerr << "async wait error " << ec << "\n";
1596 return;
1597 }
James Feist1df06a42019-04-11 14:23:04 -07001598 timerRunning = false;
James Feist8f2710a2018-05-09 17:18:55 -07001599
1600 nlohmann::json oldConfiguration = systemConfiguration;
1601 DBUS_PROBE_OBJECTS.clear();
1602
1603 std::list<nlohmann::json> configurations;
1604 if (!findJsonFiles(configurations))
1605 {
1606 std::cerr << "cannot find json files\n";
1607 return;
1608 }
1609
1610 auto perfScan = std::make_shared<PerformScan>(
1611 systemConfiguration, configurations, [&, oldConfiguration]() {
1612 nlohmann::json newConfiguration = systemConfiguration;
James Feist4131aea2018-03-09 09:47:30 -08001613 for (auto it = newConfiguration.begin();
1614 it != newConfiguration.end();)
1615 {
1616 auto findKey = oldConfiguration.find(it.key());
1617 if (findKey != oldConfiguration.end())
1618 {
1619 it = newConfiguration.erase(it);
1620 }
1621 else
1622 {
1623 it++;
1624 }
1625 }
James Feist8f2710a2018-05-09 17:18:55 -07001626 registerCallbacks(io, dbusMatches, systemConfiguration,
1627 objServer);
1628 io.post([&, newConfiguration]() {
James Feist8f2710a2018-05-09 17:18:55 -07001629 loadOverlays(newConfiguration);
James Feistce4367c2018-10-16 09:19:57 -07001630
James Feistbb43d022018-06-12 15:44:33 -07001631 io.post([&]() {
1632 if (!writeJsonFiles(systemConfiguration))
1633 {
1634 std::cerr << "Error writing json files\n";
1635 }
1636 });
James Feist8f2710a2018-05-09 17:18:55 -07001637 io.post([&, newConfiguration]() {
James Feist97a63f12018-05-17 13:50:57 -07001638 postToDbus(newConfiguration, systemConfiguration,
1639 objServer);
James Feist1df06a42019-04-11 14:23:04 -07001640 if (!timerRunning)
1641 {
1642 startRemovedTimer(timer, dbusMatches,
1643 systemConfiguration);
1644 }
James Feist8f2710a2018-05-09 17:18:55 -07001645 });
1646 });
1647 });
1648 perfScan->run();
1649 });
James Feist75fdeeb2018-02-20 14:26:16 -08001650}
1651
James Feista465ccc2019-02-08 12:51:01 -08001652void registerCallbacks(boost::asio::io_service& io,
1653 std::vector<sdbusplus::bus::match::match>& dbusMatches,
1654 nlohmann::json& systemConfiguration,
1655 sdbusplus::asio::object_server& objServer)
James Feist75fdeeb2018-02-20 14:26:16 -08001656{
1657 static boost::container::flat_set<std::string> watchedObjects;
1658
James Feista465ccc2019-02-08 12:51:01 -08001659 for (const auto& objectMap : DBUS_PROBE_OBJECTS)
James Feist75fdeeb2018-02-20 14:26:16 -08001660 {
1661 auto findObject = watchedObjects.find(objectMap.first);
1662 if (findObject != watchedObjects.end())
1663 {
1664 continue;
1665 }
James Feist8f2710a2018-05-09 17:18:55 -07001666 std::function<void(sdbusplus::message::message & message)>
1667 eventHandler =
James Feist75fdeeb2018-02-20 14:26:16 -08001668
James Feista465ccc2019-02-08 12:51:01 -08001669 [&](sdbusplus::message::message&) {
James Feist8f2710a2018-05-09 17:18:55 -07001670 propertiesChangedCallback(io, dbusMatches,
1671 systemConfiguration, objServer);
1672 };
1673
1674 sdbusplus::bus::match::match match(
James Feista465ccc2019-02-08 12:51:01 -08001675 static_cast<sdbusplus::bus::bus&>(*SYSTEM_BUS),
James Feist8f2710a2018-05-09 17:18:55 -07001676 "type='signal',member='PropertiesChanged',arg0='" +
1677 objectMap.first + "'",
1678 eventHandler);
1679 dbusMatches.emplace_back(std::move(match));
James Feist75fdeeb2018-02-20 14:26:16 -08001680 }
1681}
1682
James Feista465ccc2019-02-08 12:51:01 -08001683int main(int argc, char** argv)
James Feist75fdeeb2018-02-20 14:26:16 -08001684{
1685 // setup connection to dbus
1686 boost::asio::io_service io;
James Feist8f2710a2018-05-09 17:18:55 -07001687 SYSTEM_BUS = std::make_shared<sdbusplus::asio::connection>(io);
James Feist75fdeeb2018-02-20 14:26:16 -08001688 SYSTEM_BUS->request_name("xyz.openbmc_project.EntityManager");
James Feist4131aea2018-03-09 09:47:30 -08001689
James Feist8f2710a2018-05-09 17:18:55 -07001690 sdbusplus::asio::object_server objServer(SYSTEM_BUS);
James Feistfd1264a2018-05-03 12:10:00 -07001691
James Feist8f2710a2018-05-09 17:18:55 -07001692 std::shared_ptr<sdbusplus::asio::dbus_interface> entityIface =
1693 objServer.add_interface("/xyz/openbmc_project/EntityManager",
1694 "xyz.openbmc_project.EntityManager");
James Feistfd1264a2018-05-03 12:10:00 -07001695
James Feist8f2710a2018-05-09 17:18:55 -07001696 std::shared_ptr<sdbusplus::asio::dbus_interface> inventoryIface =
1697 objServer.add_interface("/xyz/openbmc_project/inventory",
1698 "xyz.openbmc_project.Inventory.Manager");
James Feist4131aea2018-03-09 09:47:30 -08001699
1700 // to keep reference to the match / filter objects so they don't get
1701 // destroyed
James Feist8f2710a2018-05-09 17:18:55 -07001702 std::vector<sdbusplus::bus::match::match> dbusMatches;
1703
1704 nlohmann::json systemConfiguration = nlohmann::json::object();
1705
1706 inventoryIface->register_method(
James Feista465ccc2019-02-08 12:51:01 -08001707 "Notify",
1708 [](const boost::container::flat_map<
1709 std::string,
1710 boost::container::flat_map<std::string, BasicVariantType>>&
1711 object) { return; });
James Feist8f2710a2018-05-09 17:18:55 -07001712 inventoryIface->initialize();
1713
1714 io.post([&]() {
James Feistce4367c2018-10-16 09:19:57 -07001715#if OVERLAYS
James Feist8f2710a2018-05-09 17:18:55 -07001716 unloadAllOverlays();
James Feistce4367c2018-10-16 09:19:57 -07001717#endif
James Feist8f2710a2018-05-09 17:18:55 -07001718 propertiesChangedCallback(io, dbusMatches, systemConfiguration,
1719 objServer);
1720 });
James Feist4131aea2018-03-09 09:47:30 -08001721
James Feistfd1264a2018-05-03 12:10:00 -07001722 entityIface->register_method("ReScan", [&]() {
James Feist8f2710a2018-05-09 17:18:55 -07001723 propertiesChangedCallback(io, dbusMatches, systemConfiguration,
1724 objServer);
James Feist75fdeeb2018-02-20 14:26:16 -08001725 });
James Feist8f2710a2018-05-09 17:18:55 -07001726 entityIface->initialize();
1727
James Feist1df06a42019-04-11 14:23:04 -07001728 if (fwVersionIsSame())
1729 {
1730 if (std::filesystem::is_regular_file(currentConfiguration))
1731 {
1732 // this file could just be deleted, but it's nice for debug
1733 std::filesystem::create_directory(tempConfigDir);
1734 std::filesystem::remove(lastConfiguration);
1735 std::filesystem::copy(currentConfiguration, lastConfiguration);
1736 std::filesystem::remove(currentConfiguration);
1737
1738 std::ifstream jsonStream(lastConfiguration);
1739 if (jsonStream.good())
1740 {
1741 auto data = nlohmann::json::parse(jsonStream, nullptr, false);
1742 if (data.is_discarded())
1743 {
1744 std::cerr << "syntax error in " << lastConfiguration
1745 << "\n";
1746 }
1747 else
1748 {
1749 lastJson = std::move(data);
1750 }
1751 }
1752 else
1753 {
1754 std::cerr << "unable to open " << lastConfiguration << "\n";
1755 }
1756 }
1757 }
1758 else
1759 {
1760 // not an error, just logging at this level to make it in the journal
1761 std::cerr << "Clearing previous configuration\n";
1762 std::filesystem::remove(currentConfiguration);
1763 }
1764
1765 // some boards only show up after power is on, we want to not say they are
1766 // removed until the same state happens
1767 setupPowerMatch(SYSTEM_BUS);
1768
James Feist1b2e2242018-01-30 13:45:19 -08001769 io.run();
James Feist3cb5fec2018-01-23 14:41:51 -08001770
1771 return 0;
James Feist75fdeeb2018-02-20 14:26:16 -08001772}