James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1 | /* |
| 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 Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 17 | #include "EntityManager.hpp" |
| 18 | |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 19 | #include "VariantVisitors.hpp" |
| 20 | |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 21 | #include <Overlay.hpp> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 22 | #include <Utils.hpp> |
| 23 | #include <VariantVisitors.hpp> |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 24 | #include <boost/algorithm/string/case_conv.hpp> |
James Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 25 | #include <boost/algorithm/string/classification.hpp> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 26 | #include <boost/algorithm/string/predicate.hpp> |
| 27 | #include <boost/algorithm/string/replace.hpp> |
James Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 28 | #include <boost/algorithm/string/split.hpp> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 29 | #include <boost/container/flat_map.hpp> |
| 30 | #include <boost/container/flat_set.hpp> |
James Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 31 | #include <boost/range/iterator_range.hpp> |
James Feist | 637b3ef | 2019-04-15 16:35:30 -0700 | [diff] [blame] | 32 | #include <filesystem> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 33 | #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 Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 40 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 41 | constexpr const char* configurationDirectory = PACKAGE_DIR "configurations"; |
| 42 | constexpr const char* schemaDirectory = PACKAGE_DIR "configurations/schemas"; |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 43 | constexpr const char* tempConfigDir = "/tmp/configuration/"; |
| 44 | constexpr const char* lastConfiguration = "/tmp/configuration/last.json"; |
| 45 | constexpr const char* currentConfiguration = "/var/configuration/system.json"; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 46 | constexpr const char* globalSchema = "global.json"; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 47 | constexpr const int32_t MAX_MAPPER_DEPTH = 0; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 48 | |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 49 | constexpr const bool DEBUG = false; |
| 50 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 51 | struct cmp_str |
| 52 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 53 | bool operator()(const char* a, const char* b) const |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 54 | { |
| 55 | return std::strcmp(a, b) < 0; |
| 56 | } |
| 57 | }; |
| 58 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 59 | struct PerformProbe; |
| 60 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 61 | // underscore T for collison with dbus c api |
| 62 | enum class probe_type_codes |
| 63 | { |
| 64 | FALSE_T, |
| 65 | TRUE_T, |
| 66 | AND, |
| 67 | OR, |
James Feist | 6bd2a02 | 2018-03-13 12:30:58 -0700 | [diff] [blame] | 68 | FOUND, |
| 69 | MATCH_ONE |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 70 | }; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 71 | const static boost::container::flat_map<const char*, probe_type_codes, cmp_str> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 72 | 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 Feist | 6bd2a02 | 2018-03-13 12:30:58 -0700 | [diff] [blame] | 76 | {"FOUND", probe_type_codes::FOUND}, |
| 77 | {"MATCH_ONE", probe_type_codes::MATCH_ONE}}}; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 78 | |
James Feist | 4133426 | 2019-03-25 13:30:20 -0700 | [diff] [blame] | 79 | static constexpr std::array<const char*, 5> settableInterfaces = { |
| 80 | "FanProfile", "Pid", "Pid.Zone", "Stepwise", "Thresholds"}; |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 81 | using JsonVariantType = |
James Feist | 338b8a7 | 2019-03-01 10:16:45 -0800 | [diff] [blame] | 82 | 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 Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 85 | using GetSubTreeType = std::vector< |
| 86 | std::pair<std::string, |
| 87 | std::vector<std::pair<std::string, std::vector<std::string>>>>>; |
| 88 | |
| 89 | using ManagedObjectType = boost::container::flat_map< |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 90 | sdbusplus::message::object_path, |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 91 | boost::container::flat_map< |
| 92 | std::string, |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 93 | boost::container::flat_map<std::string, BasicVariantType>>>; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 94 | |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 95 | using FoundDeviceT = |
| 96 | std::vector<boost::container::flat_map<std::string, BasicVariantType>>; |
| 97 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 98 | boost::container::flat_map< |
| 99 | std::string, |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 100 | std::vector<boost::container::flat_map<std::string, BasicVariantType>>> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 101 | DBUS_PROBE_OBJECTS; |
| 102 | std::vector<std::string> PASSED_PROBES; |
| 103 | |
| 104 | // todo: pass this through nicer |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 105 | std::shared_ptr<sdbusplus::asio::connection> SYSTEM_BUS; |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 106 | static nlohmann::json lastJson; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 107 | |
Johnathan Mantey | 2015f75 | 2019-03-26 15:22:31 -0700 | [diff] [blame] | 108 | const std::regex ILLEGAL_DBUS_PATH_REGEX("[^A-Za-z0-9_.]"); |
| 109 | const std::regex ILLEGAL_DBUS_MEMBER_REGEX("[^A-Za-z0-9_]"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 110 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 111 | void registerCallbacks(boost::asio::io_service& io, |
| 112 | std::vector<sdbusplus::bus::match::match>& dbusMatches, |
| 113 | nlohmann::json& systemConfiguration, |
| 114 | sdbusplus::asio::object_server& objServer); |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 115 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 116 | // calls the mapper to find all exposed objects of an interface type |
| 117 | // and creates a vector<flat_map> that contains all the key value pairs |
| 118 | // getManagedObjects |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 119 | void findDbusObjects(std::shared_ptr<PerformProbe> probe, |
| 120 | std::shared_ptr<sdbusplus::asio::connection> connection, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 121 | std::string& interface) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 122 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 123 | |
| 124 | // store reference to pending callbacks so we don't overwhelm services |
| 125 | static boost::container::flat_map< |
| 126 | std::string, std::vector<std::shared_ptr<PerformProbe>>> |
| 127 | pendingProbes; |
| 128 | |
| 129 | if (DBUS_PROBE_OBJECTS[interface].size()) |
| 130 | { |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | // add shared_ptr to vector of Probes waiting for callback from a specific |
| 135 | // interface to keep alive while waiting for response |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 136 | std::array<const char*, 1> objects = {interface.c_str()}; |
| 137 | std::vector<std::shared_ptr<PerformProbe>>& pending = |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 138 | pendingProbes[interface]; |
| 139 | auto iter = pending.emplace(pending.end(), probe); |
| 140 | // only allow first call to run to not overwhelm processes |
| 141 | if (iter != pending.begin()) |
| 142 | { |
| 143 | return; |
| 144 | } |
| 145 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 146 | // find all connections in the mapper that expose a specific type |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 147 | connection->async_method_call( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 148 | [connection, interface, probe](boost::system::error_code& ec, |
| 149 | const GetSubTreeType& interfaceSubtree) { |
James Feist | 0de4015 | 2018-07-25 11:56:12 -0700 | [diff] [blame] | 150 | boost::container::flat_set<std::string> interfaceConnections; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 151 | if (ec) |
James Feist | 494155a | 2018-03-14 16:23:24 -0700 | [diff] [blame] | 152 | { |
James Feist | 0de4015 | 2018-07-25 11:56:12 -0700 | [diff] [blame] | 153 | pendingProbes[interface].clear(); |
| 154 | if (ec.value() == ENOENT) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 155 | { |
James Feist | 0de4015 | 2018-07-25 11:56:12 -0700 | [diff] [blame] | 156 | return; // wasn't found by mapper |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 157 | } |
James Feist | 0de4015 | 2018-07-25 11:56:12 -0700 | [diff] [blame] | 158 | std::cerr << "Error communicating to mapper.\n"; |
| 159 | |
| 160 | // if we can't communicate to the mapper something is very wrong |
| 161 | std::exit(EXIT_FAILURE); |
James Feist | 494155a | 2018-03-14 16:23:24 -0700 | [diff] [blame] | 162 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 163 | else |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 164 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 165 | for (auto& object : interfaceSubtree) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 166 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 167 | for (auto& connPair : object.second) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 168 | { |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 169 | interfaceConnections.insert(connPair.first); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 170 | } |
| 171 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 172 | } |
James Feist | 63845bf | 2019-01-24 12:19:51 -0800 | [diff] [blame] | 173 | if (interfaceConnections.empty()) |
| 174 | { |
| 175 | pendingProbes[interface].clear(); |
| 176 | return; |
| 177 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 178 | // get managed objects for all interfaces |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 179 | for (const auto& conn : interfaceConnections) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 180 | { |
| 181 | connection->async_method_call( |
James Feist | 0de4015 | 2018-07-25 11:56:12 -0700 | [diff] [blame] | 182 | [conn, |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 183 | interface](boost::system::error_code& errc, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 184 | const ManagedObjectType& managedInterface) { |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 185 | if (errc) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 186 | { |
| 187 | std::cerr |
| 188 | << "error getting managed object for device " |
| 189 | << conn << "\n"; |
| 190 | pendingProbes[interface].clear(); |
| 191 | return; |
| 192 | } |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 193 | for (auto& interfaceManagedObj : managedInterface) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 194 | { |
| 195 | auto ifaceObjFind = |
| 196 | interfaceManagedObj.second.find(interface); |
| 197 | if (ifaceObjFind != |
| 198 | interfaceManagedObj.second.end()) |
| 199 | { |
| 200 | std::vector<boost::container::flat_map< |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 201 | std::string, BasicVariantType>>& |
| 202 | dbusObject = DBUS_PROBE_OBJECTS[interface]; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 203 | dbusObject.emplace_back(ifaceObjFind->second); |
| 204 | } |
| 205 | } |
| 206 | pendingProbes[interface].clear(); |
| 207 | }, |
| 208 | conn.c_str(), "/", "org.freedesktop.DBus.ObjectManager", |
| 209 | "GetManagedObjects"); |
| 210 | } |
| 211 | }, |
| 212 | "xyz.openbmc_project.ObjectMapper", |
| 213 | "/xyz/openbmc_project/object_mapper", |
James Feist | 5131ac2 | 2018-10-25 12:22:23 -0700 | [diff] [blame] | 214 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", MAX_MAPPER_DEPTH, |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 215 | objects); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 216 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 217 | // probes dbus interface dictionary for a key with a value that matches a regex |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 218 | bool probeDbus(const std::string& interface, |
| 219 | const std::map<std::string, nlohmann::json>& matches, |
| 220 | FoundDeviceT& devices, bool& foundProbe) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 221 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 222 | std::vector<boost::container::flat_map<std::string, BasicVariantType>>& |
| 223 | dbusObject = DBUS_PROBE_OBJECTS[interface]; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 224 | if (dbusObject.empty()) |
| 225 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 226 | foundProbe = false; |
| 227 | return false; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 228 | } |
| 229 | foundProbe = true; |
| 230 | |
| 231 | bool foundMatch = false; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 232 | for (auto& device : dbusObject) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 233 | { |
| 234 | bool deviceMatches = true; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 235 | for (auto& match : matches) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 236 | { |
| 237 | auto deviceValue = device.find(match.first); |
| 238 | if (deviceValue != device.end()) |
| 239 | { |
| 240 | switch (match.second.type()) |
| 241 | { |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 242 | case nlohmann::json::value_t::string: |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 243 | { |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 244 | std::regex search(match.second.get<std::string>()); |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 245 | std::smatch regMatch; |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 246 | |
| 247 | // convert value to string respresentation |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 248 | std::string probeValue = std::visit( |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 249 | VariantToStringVisitor(), deviceValue->second); |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 250 | if (!std::regex_search(probeValue, regMatch, search)) |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 251 | { |
| 252 | deviceMatches = false; |
| 253 | break; |
| 254 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 255 | break; |
| 256 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 257 | case nlohmann::json::value_t::boolean: |
| 258 | case nlohmann::json::value_t::number_unsigned: |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 259 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 260 | unsigned int probeValue = std::visit( |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 261 | VariantToUnsignedIntVisitor(), deviceValue->second); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 262 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 263 | if (probeValue != match.second.get<unsigned int>()) |
| 264 | { |
| 265 | deviceMatches = false; |
| 266 | } |
| 267 | break; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 268 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 269 | case nlohmann::json::value_t::number_integer: |
| 270 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 271 | int probeValue = std::visit(VariantToIntVisitor(), |
| 272 | deviceValue->second); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 273 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 274 | if (probeValue != match.second.get<int>()) |
| 275 | { |
| 276 | deviceMatches = false; |
| 277 | } |
| 278 | break; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 279 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 280 | case nlohmann::json::value_t::number_float: |
| 281 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 282 | float probeValue = std::visit(VariantToFloatVisitor(), |
| 283 | deviceValue->second); |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 284 | |
| 285 | if (probeValue != match.second.get<float>()) |
| 286 | { |
| 287 | deviceMatches = false; |
| 288 | } |
| 289 | break; |
| 290 | } |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 291 | default: |
| 292 | { |
| 293 | std::cerr << "unexpected dbus probe type " |
| 294 | << match.second.type_name() << "\n"; |
| 295 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | else |
| 299 | { |
| 300 | deviceMatches = false; |
| 301 | break; |
| 302 | } |
| 303 | } |
| 304 | if (deviceMatches) |
| 305 | { |
James Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 306 | devices.emplace_back(device); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 307 | 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 |
| 316 | bool probe( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 317 | const std::vector<std::string>& probeCommand, |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 318 | std::vector<boost::container::flat_map<std::string, BasicVariantType>>& |
| 319 | foundDevs) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 320 | { |
| 321 | const static std::regex command(R"(\((.*)\))"); |
| 322 | std::smatch match; |
| 323 | bool ret = false; |
James Feist | 6bd2a02 | 2018-03-13 12:30:58 -0700 | [diff] [blame] | 324 | bool matchOne = false; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 325 | bool cur = true; |
| 326 | probe_type_codes lastCommand = probe_type_codes::FALSE_T; |
James Feist | 54a0dca | 2019-06-26 10:34:54 -0700 | [diff] [blame] | 327 | bool first = true; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 328 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 329 | for (auto& probe : probeCommand) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 330 | { |
| 331 | bool foundProbe = false; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 332 | boost::container::flat_map<const char*, probe_type_codes, |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 333 | 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 Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 348 | case probe_type_codes::FALSE_T: |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 349 | { |
James Feist | e31e00a | 2019-07-24 10:45:43 -0700 | [diff] [blame] | 350 | cur = false; |
| 351 | break; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 352 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 353 | case probe_type_codes::TRUE_T: |
| 354 | { |
James Feist | e31e00a | 2019-07-24 10:45:43 -0700 | [diff] [blame] | 355 | cur = true; |
| 356 | break; |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 357 | } |
| 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 Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 376 | std::cerr << "found probe syntax error " << probe |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 377 | << "\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 Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 386 | default: |
| 387 | { |
| 388 | break; |
| 389 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | // look on dbus for object |
| 393 | else |
| 394 | { |
| 395 | if (!std::regex_search(probe, match, command)) |
| 396 | { |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 397 | std::cerr << "dbus probe syntax error " << probe << "\n"; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 398 | return false; |
| 399 | } |
| 400 | std::string commandStr = *(match.begin() + 1); |
| 401 | // convert single ticks and single slashes into legal json |
Jae Hyun Yoo | 3936e7a | 2018-03-23 17:26:16 -0700 | [diff] [blame] | 402 | boost::replace_all(commandStr, "'", "\""); |
James Feist | 3f8a278 | 2018-02-12 09:24:42 -0800 | [diff] [blame] | 403 | boost::replace_all(commandStr, R"(\)", R"(\\)"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 404 | auto json = nlohmann::json::parse(commandStr, nullptr, false); |
| 405 | if (json.is_discarded()) |
| 406 | { |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 407 | std::cerr << "dbus command syntax error " << commandStr << "\n"; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 408 | 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 Feist | 54a0dca | 2019-06-26 10:34:54 -0700 | [diff] [blame] | 426 | if (lastCommand == probe_type_codes::AND) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 427 | { |
James Feist | 54a0dca | 2019-06-26 10:34:54 -0700 | [diff] [blame] | 428 | 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 Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 439 | } |
| 440 | lastCommand = probeType != PROBE_TYPES.end() |
| 441 | ? probeType->second |
| 442 | : probe_type_codes::FALSE_T; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | // probe passed, but empty device |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 446 | if (ret && foundDevs.size() == 0) |
| 447 | { |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 448 | foundDevs.emplace_back( |
| 449 | boost::container::flat_map<std::string, BasicVariantType>{}); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 450 | } |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 451 | if (matchOne && ret) |
James Feist | 6bd2a02 | 2018-03-13 12:30:58 -0700 | [diff] [blame] | 452 | { |
James Feist | 71f295f | 2019-06-20 13:35:12 -0700 | [diff] [blame] | 453 | // match the last one |
| 454 | auto last = foundDevs.back(); |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 455 | foundDevs.clear(); |
James Feist | 71f295f | 2019-06-20 13:35:12 -0700 | [diff] [blame] | 456 | |
| 457 | foundDevs.emplace_back(std::move(last)); |
James Feist | 6bd2a02 | 2018-03-13 12:30:58 -0700 | [diff] [blame] | 458 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 459 | return ret; |
| 460 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 461 | // this class finds the needed dbus fields and on destruction runs the probe |
| 462 | struct PerformProbe : std::enable_shared_from_this<PerformProbe> |
| 463 | { |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 464 | |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 465 | PerformProbe(const std::vector<std::string>& probeCommand, |
| 466 | std::function<void(FoundDeviceT&)>&& callback) : |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 467 | _probeCommand(probeCommand), |
| 468 | _callback(std::move(callback)) |
| 469 | { |
| 470 | } |
| 471 | ~PerformProbe() |
| 472 | { |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 473 | FoundDeviceT foundDevs; |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 474 | if (probe(_probeCommand, foundDevs)) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 475 | { |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 476 | _callback(foundDevs); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | void run() |
| 480 | { |
| 481 | // parse out dbus probes by discarding other probe types |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 482 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 483 | for (std::string& probe : _probeCommand) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 484 | { |
| 485 | bool found = false; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 486 | boost::container::flat_map<const char*, probe_type_codes, |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 487 | cmp_str>::const_iterator probeType; |
| 488 | for (probeType = PROBE_TYPES.begin(); |
| 489 | probeType != PROBE_TYPES.end(); probeType++) |
| 490 | { |
| 491 | if (probe.find(probeType->first) != std::string::npos) |
| 492 | { |
| 493 | found = true; |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | if (found) |
| 498 | { |
| 499 | continue; |
| 500 | } |
| 501 | // syntax requires probe before first open brace |
| 502 | auto findStart = probe.find("("); |
| 503 | std::string interface = probe.substr(0, findStart); |
| 504 | |
| 505 | findDbusObjects(shared_from_this(), SYSTEM_BUS, interface); |
| 506 | } |
| 507 | } |
| 508 | std::vector<std::string> _probeCommand; |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 509 | std::function<void(FoundDeviceT&)> _callback; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 510 | }; |
| 511 | |
| 512 | // writes output files to persist data |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 513 | bool writeJsonFiles(const nlohmann::json& systemConfiguration) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 514 | { |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 515 | std::filesystem::create_directory(configurationOutDir); |
| 516 | std::ofstream output(currentConfiguration); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 517 | if (!output.good()) |
| 518 | { |
| 519 | return false; |
| 520 | } |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 521 | output << systemConfiguration.dump(4); |
| 522 | output.close(); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 523 | return true; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 524 | } |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 525 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 526 | template <typename JsonType> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 527 | bool setJsonFromPointer(const std::string& ptrStr, const JsonType& value, |
| 528 | nlohmann::json& systemConfiguration) |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 529 | { |
| 530 | try |
| 531 | { |
| 532 | nlohmann::json::json_pointer ptr(ptrStr); |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 533 | nlohmann::json& ref = systemConfiguration[ptr]; |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 534 | ref = value; |
| 535 | return true; |
| 536 | } |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 537 | catch (const std::out_of_range&) |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 538 | { |
| 539 | return false; |
| 540 | } |
| 541 | } |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 542 | |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 543 | // template function to add array as dbus property |
| 544 | template <typename PropertyType> |
| 545 | void addArrayToDbus(const std::string& name, const nlohmann::json& array, |
| 546 | sdbusplus::asio::dbus_interface* iface, |
| 547 | sdbusplus::asio::PropertyPermission permission, |
| 548 | nlohmann::json& systemConfiguration, |
| 549 | const std::string& jsonPointerString) |
| 550 | { |
| 551 | std::vector<PropertyType> values; |
| 552 | for (const auto& property : array) |
| 553 | { |
| 554 | auto ptr = property.get_ptr<const PropertyType*>(); |
| 555 | if (ptr != nullptr) |
| 556 | { |
| 557 | values.emplace_back(*ptr); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | if (permission == sdbusplus::asio::PropertyPermission::readOnly) |
| 562 | { |
| 563 | iface->register_property(name, values); |
| 564 | } |
| 565 | else |
| 566 | { |
| 567 | iface->register_property( |
| 568 | name, values, |
| 569 | [&systemConfiguration, |
| 570 | jsonPointerString{std::string(jsonPointerString)}]( |
| 571 | const std::vector<PropertyType>& newVal, |
| 572 | std::vector<PropertyType>& val) { |
| 573 | val = newVal; |
| 574 | if (!setJsonFromPointer(jsonPointerString, val, |
| 575 | systemConfiguration)) |
| 576 | { |
| 577 | std::cerr << "error setting json field\n"; |
| 578 | return -1; |
| 579 | } |
| 580 | if (!writeJsonFiles(systemConfiguration)) |
| 581 | { |
| 582 | std::cerr << "error setting json file\n"; |
| 583 | return -1; |
| 584 | } |
| 585 | return 1; |
| 586 | }); |
| 587 | } |
| 588 | } |
| 589 | |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 590 | template <typename PropertyType> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 591 | void addProperty(const std::string& propertyName, const PropertyType& value, |
| 592 | sdbusplus::asio::dbus_interface* iface, |
| 593 | nlohmann::json& systemConfiguration, |
| 594 | const std::string& jsonPointerString, |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 595 | sdbusplus::asio::PropertyPermission permission) |
| 596 | { |
| 597 | if (permission == sdbusplus::asio::PropertyPermission::readOnly) |
| 598 | { |
| 599 | iface->register_property(propertyName, value); |
| 600 | return; |
| 601 | } |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 602 | iface->register_property( |
| 603 | propertyName, value, |
| 604 | [&systemConfiguration, |
| 605 | jsonPointerString{std::string(jsonPointerString)}]( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 606 | const PropertyType& newVal, PropertyType& val) { |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 607 | val = newVal; |
| 608 | if (!setJsonFromPointer(jsonPointerString, val, |
| 609 | systemConfiguration)) |
| 610 | { |
| 611 | std::cerr << "error setting json field\n"; |
| 612 | return -1; |
| 613 | } |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 614 | if (!writeJsonFiles(systemConfiguration)) |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 615 | { |
| 616 | std::cerr << "error setting json file\n"; |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 617 | return -1; |
| 618 | } |
| 619 | return 1; |
| 620 | }); |
| 621 | } |
| 622 | |
| 623 | void createDeleteObjectMethod( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 624 | const std::string& jsonPointerPath, |
| 625 | const std::shared_ptr<sdbusplus::asio::dbus_interface>& iface, |
| 626 | sdbusplus::asio::object_server& objServer, |
| 627 | nlohmann::json& systemConfiguration) |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 628 | { |
| 629 | std::weak_ptr<sdbusplus::asio::dbus_interface> interface = iface; |
| 630 | iface->register_method( |
| 631 | "Delete", [&objServer, &systemConfiguration, interface, |
| 632 | jsonPointerPath{std::string(jsonPointerPath)}]() { |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 633 | std::shared_ptr<sdbusplus::asio::dbus_interface> dbusInterface = |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 634 | interface.lock(); |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 635 | if (!dbusInterface) |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 636 | { |
| 637 | // this technically can't happen as the pointer is pointing to |
| 638 | // us |
| 639 | throw DBusInternalError(); |
| 640 | } |
| 641 | nlohmann::json::json_pointer ptr(jsonPointerPath); |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 642 | if (!objServer.remove_interface(dbusInterface)) |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 643 | { |
| 644 | std::cerr << "Can't delete interface " << jsonPointerPath |
| 645 | << "\n"; |
| 646 | throw DBusInternalError(); |
| 647 | } |
| 648 | systemConfiguration[ptr] = nullptr; |
| 649 | |
| 650 | if (!writeJsonFiles(systemConfiguration)) |
| 651 | { |
| 652 | std::cerr << "error setting json file\n"; |
| 653 | throw DBusInternalError(); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 654 | } |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 655 | return -1; |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 656 | }); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 657 | } |
| 658 | |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 659 | // adds simple json types to interface's properties |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 660 | void populateInterfaceFromJson( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 661 | nlohmann::json& systemConfiguration, const std::string& jsonPointerPath, |
| 662 | std::shared_ptr<sdbusplus::asio::dbus_interface>& iface, |
| 663 | nlohmann::json& dict, sdbusplus::asio::object_server& objServer, |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 664 | sdbusplus::asio::PropertyPermission permission = |
| 665 | sdbusplus::asio::PropertyPermission::readOnly) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 666 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 667 | for (auto& dictPair : dict.items()) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 668 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 669 | auto type = dictPair.value().type(); |
| 670 | bool array = false; |
| 671 | if (dictPair.value().type() == nlohmann::json::value_t::array) |
| 672 | { |
| 673 | array = true; |
| 674 | if (!dictPair.value().size()) |
| 675 | { |
| 676 | continue; |
| 677 | } |
| 678 | type = dictPair.value()[0].type(); |
| 679 | bool isLegal = true; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 680 | for (const auto& arrayItem : dictPair.value()) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 681 | { |
| 682 | if (arrayItem.type() != type) |
| 683 | { |
| 684 | isLegal = false; |
| 685 | break; |
| 686 | } |
| 687 | } |
| 688 | if (!isLegal) |
| 689 | { |
| 690 | std::cerr << "dbus format error" << dictPair.value() << "\n"; |
| 691 | continue; |
| 692 | } |
James Feist | a218ddb | 2019-04-11 14:01:31 -0700 | [diff] [blame] | 693 | } |
| 694 | if (type == nlohmann::json::value_t::object) |
| 695 | { |
| 696 | continue; // handled elsewhere |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 697 | } |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 698 | std::string key = jsonPointerPath + "/" + dictPair.key(); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 699 | if (permission == sdbusplus::asio::PropertyPermission::readWrite) |
| 700 | { |
| 701 | // all setable numbers are doubles as it is difficult to always |
| 702 | // create a configuration file with all whole numbers as decimals |
| 703 | // i.e. 1.0 |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 704 | if (array) |
| 705 | { |
| 706 | if (dictPair.value()[0].is_number()) |
| 707 | { |
| 708 | type = nlohmann::json::value_t::number_float; |
| 709 | } |
| 710 | } |
| 711 | else if (dictPair.value().is_number()) |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 712 | { |
| 713 | type = nlohmann::json::value_t::number_float; |
| 714 | } |
| 715 | } |
| 716 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 717 | switch (type) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 718 | { |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 719 | case (nlohmann::json::value_t::boolean): |
| 720 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 721 | if (array) |
| 722 | { |
| 723 | // todo: array of bool isn't detected correctly by |
| 724 | // sdbusplus, change it to numbers |
| 725 | addArrayToDbus<uint64_t>(dictPair.key(), dictPair.value(), |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 726 | iface.get(), permission, |
| 727 | systemConfiguration, key); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 728 | } |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 729 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 730 | else |
| 731 | { |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 732 | addProperty(dictPair.key(), dictPair.value().get<bool>(), |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 733 | iface.get(), systemConfiguration, key, |
| 734 | permission); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 735 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 736 | break; |
| 737 | } |
| 738 | case (nlohmann::json::value_t::number_integer): |
| 739 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 740 | if (array) |
| 741 | { |
| 742 | addArrayToDbus<int64_t>(dictPair.key(), dictPair.value(), |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 743 | iface.get(), permission, |
| 744 | systemConfiguration, key); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 745 | } |
| 746 | else |
| 747 | { |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 748 | addProperty(dictPair.key(), dictPair.value().get<int64_t>(), |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 749 | iface.get(), systemConfiguration, key, |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 750 | sdbusplus::asio::PropertyPermission::readOnly); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 751 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 752 | break; |
| 753 | } |
| 754 | case (nlohmann::json::value_t::number_unsigned): |
| 755 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 756 | if (array) |
| 757 | { |
| 758 | addArrayToDbus<uint64_t>(dictPair.key(), dictPair.value(), |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 759 | iface.get(), permission, |
| 760 | systemConfiguration, key); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 761 | } |
| 762 | else |
| 763 | { |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 764 | addProperty(dictPair.key(), |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 765 | dictPair.value().get<uint64_t>(), iface.get(), |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 766 | systemConfiguration, key, |
| 767 | sdbusplus::asio::PropertyPermission::readOnly); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 768 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 769 | break; |
| 770 | } |
| 771 | case (nlohmann::json::value_t::number_float): |
| 772 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 773 | if (array) |
| 774 | { |
| 775 | addArrayToDbus<double>(dictPair.key(), dictPair.value(), |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 776 | iface.get(), permission, |
| 777 | systemConfiguration, key); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 778 | } |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 779 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 780 | else |
| 781 | { |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 782 | addProperty(dictPair.key(), dictPair.value().get<double>(), |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 783 | iface.get(), systemConfiguration, key, |
| 784 | permission); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 785 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 786 | break; |
| 787 | } |
| 788 | case (nlohmann::json::value_t::string): |
| 789 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 790 | if (array) |
| 791 | { |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 792 | addArrayToDbus<std::string>( |
| 793 | dictPair.key(), dictPair.value(), iface.get(), |
| 794 | permission, systemConfiguration, key); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 795 | } |
| 796 | else |
| 797 | { |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 798 | addProperty( |
| 799 | dictPair.key(), dictPair.value().get<std::string>(), |
| 800 | iface.get(), systemConfiguration, key, permission); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 801 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 802 | break; |
| 803 | } |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 804 | default: |
| 805 | { |
James Feist | a218ddb | 2019-04-11 14:01:31 -0700 | [diff] [blame] | 806 | std::cerr << "Unexpected json type in system configuration " |
| 807 | << dictPair.key() << ": " |
| 808 | << dictPair.value().type_name() << "\n"; |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 809 | break; |
| 810 | } |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 811 | } |
| 812 | } |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 813 | if (permission == sdbusplus::asio::PropertyPermission::readWrite) |
| 814 | { |
| 815 | createDeleteObjectMethod(jsonPointerPath, iface, objServer, |
| 816 | systemConfiguration); |
| 817 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 818 | iface->initialize(); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 819 | } |
| 820 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 821 | sdbusplus::asio::PropertyPermission getPermission(const std::string& interface) |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 822 | { |
| 823 | return std::find(settableInterfaces.begin(), settableInterfaces.end(), |
| 824 | interface) != settableInterfaces.end() |
| 825 | ? sdbusplus::asio::PropertyPermission::readWrite |
| 826 | : sdbusplus::asio::PropertyPermission::readOnly; |
| 827 | } |
| 828 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 829 | void createAddObjectMethod(const std::string& jsonPointerPath, |
| 830 | const std::string& path, |
| 831 | nlohmann::json& systemConfiguration, |
| 832 | sdbusplus::asio::object_server& objServer) |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 833 | { |
| 834 | auto iface = objServer.add_interface(path, "xyz.openbmc_project.AddObject"); |
| 835 | |
| 836 | iface->register_method( |
| 837 | "AddObject", |
| 838 | [&systemConfiguration, &objServer, |
| 839 | jsonPointerPath{std::string(jsonPointerPath)}, |
| 840 | path{std::string(path)}]( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 841 | const boost::container::flat_map<std::string, JsonVariantType>& |
| 842 | data) { |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 843 | nlohmann::json::json_pointer ptr(jsonPointerPath); |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 844 | nlohmann::json& base = systemConfiguration[ptr]; |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 845 | auto findExposes = base.find("Exposes"); |
| 846 | |
| 847 | if (findExposes == base.end()) |
| 848 | { |
| 849 | throw std::invalid_argument("Entity must have children."); |
| 850 | } |
| 851 | |
| 852 | // this will throw invalid-argument to sdbusplus if invalid json |
| 853 | nlohmann::json newData{}; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 854 | for (const auto& item : data) |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 855 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 856 | nlohmann::json& newJson = newData[item.first]; |
| 857 | std::visit([&newJson](auto&& val) { newJson = std::move(val); }, |
| 858 | item.second); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | auto findName = newData.find("Name"); |
| 862 | auto findType = newData.find("Type"); |
| 863 | if (findName == newData.end() || findType == newData.end()) |
| 864 | { |
| 865 | throw std::invalid_argument("AddObject missing Name or Type"); |
| 866 | } |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 867 | const std::string* type = findType->get_ptr<const std::string*>(); |
| 868 | const std::string* name = findName->get_ptr<const std::string*>(); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 869 | if (type == nullptr || name == nullptr) |
| 870 | { |
| 871 | throw std::invalid_argument("Type and Name must be a string."); |
| 872 | } |
| 873 | |
| 874 | size_t lastIndex = 0; |
| 875 | // we add in the "exposes" |
| 876 | for (; lastIndex < findExposes->size(); lastIndex++) |
| 877 | { |
| 878 | if (findExposes->at(lastIndex)["Name"] == *name && |
| 879 | findExposes->at(lastIndex)["Type"] == *type) |
| 880 | { |
| 881 | throw std::invalid_argument( |
| 882 | "Field already in JSON, not adding"); |
| 883 | } |
| 884 | lastIndex++; |
| 885 | } |
| 886 | |
| 887 | std::ifstream schemaFile(std::string(schemaDirectory) + "/" + |
| 888 | *type + ".json"); |
| 889 | // todo(james) we might want to also make a list of 'can add' |
| 890 | // interfaces but for now I think the assumption if there is a |
| 891 | // schema avaliable that it is allowed to update is fine |
| 892 | if (!schemaFile.good()) |
| 893 | { |
| 894 | throw std::invalid_argument( |
| 895 | "No schema avaliable, cannot validate."); |
| 896 | } |
| 897 | nlohmann::json schema = |
| 898 | nlohmann::json::parse(schemaFile, nullptr, false); |
| 899 | if (schema.is_discarded()) |
| 900 | { |
| 901 | std::cerr << "Schema not legal" << *type << ".json\n"; |
| 902 | throw DBusInternalError(); |
| 903 | } |
| 904 | if (!validateJson(schema, newData)) |
| 905 | { |
| 906 | throw std::invalid_argument("Data does not match schema"); |
| 907 | } |
| 908 | |
James Feist | 16a02f2 | 2019-05-13 15:21:37 -0700 | [diff] [blame] | 909 | findExposes->push_back(newData); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 910 | if (!writeJsonFiles(systemConfiguration)) |
| 911 | { |
| 912 | std::cerr << "Error writing json files\n"; |
| 913 | throw DBusInternalError(); |
| 914 | } |
| 915 | std::string dbusName = *name; |
| 916 | |
| 917 | std::regex_replace(dbusName.begin(), dbusName.begin(), |
Johnathan Mantey | 2015f75 | 2019-03-26 15:22:31 -0700 | [diff] [blame] | 918 | dbusName.end(), ILLEGAL_DBUS_MEMBER_REGEX, "_"); |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 919 | auto interface = objServer.add_interface( |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 920 | path + "/" + dbusName, |
| 921 | "xyz.openbmc_project.Configuration." + *type); |
| 922 | // permission is read-write, as since we just created it, must be |
| 923 | // runtime modifiable |
| 924 | populateInterfaceFromJson( |
| 925 | systemConfiguration, |
James Feist | 16a02f2 | 2019-05-13 15:21:37 -0700 | [diff] [blame] | 926 | jsonPointerPath + "/Exposes/" + std::to_string(lastIndex), |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 927 | interface, newData, objServer, |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 928 | sdbusplus::asio::PropertyPermission::readWrite); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 929 | }); |
| 930 | iface->initialize(); |
| 931 | } |
| 932 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 933 | void postToDbus(const nlohmann::json& newConfiguration, |
| 934 | nlohmann::json& systemConfiguration, |
| 935 | sdbusplus::asio::object_server& objServer) |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 936 | |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 937 | { |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 938 | // iterate through boards |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 939 | for (auto& boardPair : newConfiguration.items()) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 940 | { |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 941 | std::string boardKey = boardPair.value()["Name"]; |
| 942 | std::string jsonPointerPath = "/" + boardPair.key(); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 943 | // loop through newConfiguration, but use values from system |
| 944 | // configuration to be able to modify via dbus later |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 945 | auto boardValues = systemConfiguration[boardPair.key()]; |
James Feist | d63d18a | 2018-07-19 15:23:45 -0700 | [diff] [blame] | 946 | auto findBoardType = boardValues.find("Type"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 947 | std::string boardType; |
| 948 | if (findBoardType != boardValues.end() && |
| 949 | findBoardType->type() == nlohmann::json::value_t::string) |
| 950 | { |
| 951 | boardType = findBoardType->get<std::string>(); |
| 952 | std::regex_replace(boardType.begin(), boardType.begin(), |
Johnathan Mantey | 2015f75 | 2019-03-26 15:22:31 -0700 | [diff] [blame] | 953 | boardType.end(), ILLEGAL_DBUS_MEMBER_REGEX, "_"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 954 | } |
| 955 | else |
| 956 | { |
| 957 | std::cerr << "Unable to find type for " << boardKey |
| 958 | << " reverting to Chassis.\n"; |
| 959 | boardType = "Chassis"; |
| 960 | } |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 961 | std::string boardtypeLower = boost::algorithm::to_lower_copy(boardType); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 962 | |
| 963 | std::regex_replace(boardKey.begin(), boardKey.begin(), boardKey.end(), |
Johnathan Mantey | 2015f75 | 2019-03-26 15:22:31 -0700 | [diff] [blame] | 964 | ILLEGAL_DBUS_MEMBER_REGEX, "_"); |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 965 | std::string boardName = "/xyz/openbmc_project/inventory/system/" + |
| 966 | boardtypeLower + "/" + boardKey; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 967 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 968 | auto inventoryIface = objServer.add_interface( |
| 969 | boardName, "xyz.openbmc_project.Inventory.Item"); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 970 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 971 | auto boardIface = objServer.add_interface( |
| 972 | boardName, "xyz.openbmc_project.Inventory.Item." + boardType); |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 973 | |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 974 | createAddObjectMethod(jsonPointerPath, boardName, systemConfiguration, |
| 975 | objServer); |
| 976 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 977 | populateInterfaceFromJson(systemConfiguration, jsonPointerPath, |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 978 | boardIface, boardValues, objServer); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 979 | jsonPointerPath += "/"; |
| 980 | // iterate through board properties |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 981 | for (auto& boardField : boardValues.items()) |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 982 | { |
| 983 | if (boardField.value().type() == nlohmann::json::value_t::object) |
| 984 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 985 | auto iface = |
| 986 | objServer.add_interface(boardName, boardField.key()); |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 987 | populateInterfaceFromJson(systemConfiguration, |
| 988 | jsonPointerPath + boardField.key(), |
| 989 | iface, boardField.value(), objServer); |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 990 | } |
| 991 | } |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 992 | |
James Feist | 1e3e698 | 2018-08-03 16:09:28 -0700 | [diff] [blame] | 993 | auto exposes = boardValues.find("Exposes"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 994 | if (exposes == boardValues.end()) |
| 995 | { |
| 996 | continue; |
| 997 | } |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 998 | // iterate through exposes |
James Feist | 1e3e698 | 2018-08-03 16:09:28 -0700 | [diff] [blame] | 999 | jsonPointerPath += "Exposes/"; |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 1000 | |
| 1001 | // store the board level pointer so we can modify it on the way down |
| 1002 | std::string jsonPointerPathBoard = jsonPointerPath; |
| 1003 | size_t exposesIndex = -1; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1004 | for (auto& item : *exposes) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1005 | { |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 1006 | exposesIndex++; |
| 1007 | jsonPointerPath = jsonPointerPathBoard; |
| 1008 | jsonPointerPath += std::to_string(exposesIndex); |
| 1009 | |
James Feist | d63d18a | 2018-07-19 15:23:45 -0700 | [diff] [blame] | 1010 | auto findName = item.find("Name"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1011 | if (findName == item.end()) |
| 1012 | { |
| 1013 | std::cerr << "cannot find name in field " << item << "\n"; |
| 1014 | continue; |
| 1015 | } |
James Feist | 1e3e698 | 2018-08-03 16:09:28 -0700 | [diff] [blame] | 1016 | auto findStatus = item.find("Status"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1017 | // if status is not found it is assumed to be status = 'okay' |
| 1018 | if (findStatus != item.end()) |
| 1019 | { |
| 1020 | if (*findStatus == "disabled") |
| 1021 | { |
| 1022 | continue; |
| 1023 | } |
| 1024 | } |
James Feist | d63d18a | 2018-07-19 15:23:45 -0700 | [diff] [blame] | 1025 | auto findType = item.find("Type"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1026 | std::string itemType; |
| 1027 | if (findType != item.end()) |
| 1028 | { |
| 1029 | itemType = findType->get<std::string>(); |
| 1030 | std::regex_replace(itemType.begin(), itemType.begin(), |
Johnathan Mantey | 2015f75 | 2019-03-26 15:22:31 -0700 | [diff] [blame] | 1031 | itemType.end(), ILLEGAL_DBUS_PATH_REGEX, |
| 1032 | "_"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1033 | } |
| 1034 | else |
| 1035 | { |
| 1036 | itemType = "unknown"; |
| 1037 | } |
| 1038 | std::string itemName = findName->get<std::string>(); |
| 1039 | std::regex_replace(itemName.begin(), itemName.begin(), |
Johnathan Mantey | 2015f75 | 2019-03-26 15:22:31 -0700 | [diff] [blame] | 1040 | itemName.end(), ILLEGAL_DBUS_MEMBER_REGEX, "_"); |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 1041 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1042 | auto itemIface = objServer.add_interface( |
| 1043 | boardName + "/" + itemName, |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1044 | "xyz.openbmc_project.Configuration." + itemType); |
| 1045 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 1046 | populateInterfaceFromJson(systemConfiguration, jsonPointerPath, |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 1047 | itemIface, item, objServer, |
| 1048 | getPermission(itemType)); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1049 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1050 | for (auto& objectPair : item.items()) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1051 | { |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 1052 | jsonPointerPath = jsonPointerPathBoard + |
| 1053 | std::to_string(exposesIndex) + "/" + |
| 1054 | objectPair.key(); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1055 | if (objectPair.value().type() == |
| 1056 | nlohmann::json::value_t::object) |
| 1057 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1058 | auto objectIface = objServer.add_interface( |
| 1059 | boardName + "/" + itemName, |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1060 | "xyz.openbmc_project.Configuration." + itemType + "." + |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1061 | objectPair.key()); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 1062 | |
| 1063 | populateInterfaceFromJson( |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 1064 | systemConfiguration, jsonPointerPath, objectIface, |
| 1065 | objectPair.value(), objServer, getPermission(itemType)); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1066 | } |
| 1067 | else if (objectPair.value().type() == |
| 1068 | nlohmann::json::value_t::array) |
| 1069 | { |
| 1070 | size_t index = 0; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1071 | if (!objectPair.value().size()) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1072 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1073 | continue; |
| 1074 | } |
| 1075 | bool isLegal = true; |
| 1076 | auto type = objectPair.value()[0].type(); |
| 1077 | if (type != nlohmann::json::value_t::object) |
| 1078 | { |
| 1079 | continue; |
| 1080 | } |
| 1081 | |
| 1082 | // verify legal json |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1083 | for (const auto& arrayItem : objectPair.value()) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1084 | { |
| 1085 | if (arrayItem.type() != type) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1086 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1087 | isLegal = false; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1088 | break; |
| 1089 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1090 | } |
| 1091 | if (!isLegal) |
| 1092 | { |
| 1093 | std::cerr << "dbus format error" << objectPair.value() |
| 1094 | << "\n"; |
| 1095 | break; |
| 1096 | } |
| 1097 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1098 | for (auto& arrayItem : objectPair.value()) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1099 | { |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 1100 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1101 | auto objectIface = objServer.add_interface( |
| 1102 | boardName + "/" + itemName, |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1103 | "xyz.openbmc_project.Configuration." + itemType + |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 1104 | "." + objectPair.key() + std::to_string(index)); |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 1105 | populateInterfaceFromJson( |
| 1106 | systemConfiguration, |
| 1107 | jsonPointerPath + "/" + std::to_string(index), |
| 1108 | objectIface, arrayItem, objServer, |
| 1109 | getPermission(objectPair.key())); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 1110 | index++; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1111 | } |
| 1112 | } |
| 1113 | } |
| 1114 | } |
| 1115 | } |
| 1116 | } |
| 1117 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1118 | // reads json files out of the filesystem |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1119 | bool findJsonFiles(std::list<nlohmann::json>& configurations) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1120 | { |
| 1121 | // find configuration files |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 1122 | std::vector<std::filesystem::path> jsonPaths; |
Johnathan Mantey | 2015f75 | 2019-03-26 15:22:31 -0700 | [diff] [blame] | 1123 | if (!findFiles(std::filesystem::path(configurationDirectory), R"(.*\.json)", |
| 1124 | jsonPaths)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1125 | { |
| 1126 | std::cerr << "Unable to find any configuration files in " |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 1127 | << configurationDirectory << "\n"; |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1128 | return false; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1129 | } |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 1130 | |
| 1131 | std::ifstream schemaStream(std::string(schemaDirectory) + "/" + |
| 1132 | globalSchema); |
| 1133 | if (!schemaStream.good()) |
| 1134 | { |
| 1135 | std::cerr |
| 1136 | << "Cannot open schema file, cannot validate JSON, exiting\n\n"; |
| 1137 | std::exit(EXIT_FAILURE); |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 1138 | return false; |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 1139 | } |
| 1140 | nlohmann::json schema = nlohmann::json::parse(schemaStream, nullptr, false); |
| 1141 | if (schema.is_discarded()) |
| 1142 | { |
| 1143 | std::cerr |
| 1144 | << "Illegal schema file detected, cannot validate JSON, exiting\n"; |
| 1145 | std::exit(EXIT_FAILURE); |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 1146 | return false; |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1149 | for (auto& jsonPath : jsonPaths) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1150 | { |
| 1151 | std::ifstream jsonStream(jsonPath.c_str()); |
| 1152 | if (!jsonStream.good()) |
| 1153 | { |
| 1154 | std::cerr << "unable to open " << jsonPath.string() << "\n"; |
| 1155 | continue; |
| 1156 | } |
| 1157 | auto data = nlohmann::json::parse(jsonStream, nullptr, false); |
| 1158 | if (data.is_discarded()) |
| 1159 | { |
| 1160 | std::cerr << "syntax error in " << jsonPath.string() << "\n"; |
| 1161 | continue; |
| 1162 | } |
James Feist | 8da9919 | 2019-01-24 08:20:16 -0800 | [diff] [blame] | 1163 | /* |
| 1164 | * todo(james): reenable this once less things are in flight |
| 1165 | * |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 1166 | if (!validateJson(schema, data)) |
| 1167 | { |
| 1168 | std::cerr << "Error validating " << jsonPath.string() << "\n"; |
| 1169 | continue; |
| 1170 | } |
James Feist | 8da9919 | 2019-01-24 08:20:16 -0800 | [diff] [blame] | 1171 | */ |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 1172 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1173 | if (data.type() == nlohmann::json::value_t::array) |
| 1174 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1175 | for (auto& d : data) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1176 | { |
| 1177 | configurations.emplace_back(d); |
| 1178 | } |
| 1179 | } |
| 1180 | else |
| 1181 | { |
| 1182 | configurations.emplace_back(data); |
| 1183 | } |
| 1184 | } |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 1185 | return true; |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1186 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1187 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1188 | struct PerformScan : std::enable_shared_from_this<PerformScan> |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1189 | { |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1190 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1191 | PerformScan(nlohmann::json& systemConfiguration, |
| 1192 | std::list<nlohmann::json>& configurations, |
| 1193 | std::function<void(void)>&& callback) : |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1194 | _systemConfiguration(systemConfiguration), |
| 1195 | _configurations(configurations), _callback(std::move(callback)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1196 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1197 | } |
| 1198 | void run() |
| 1199 | { |
| 1200 | for (auto it = _configurations.begin(); it != _configurations.end();) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1201 | { |
James Feist | 1e3e698 | 2018-08-03 16:09:28 -0700 | [diff] [blame] | 1202 | auto findProbe = it->find("Probe"); |
James Feist | d63d18a | 2018-07-19 15:23:45 -0700 | [diff] [blame] | 1203 | auto findName = it->find("Name"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1204 | |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1205 | nlohmann::json probeCommand; |
| 1206 | // check for poorly formatted fields, probe must be an array |
| 1207 | if (findProbe == it->end()) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1208 | { |
| 1209 | std::cerr << "configuration file missing probe:\n " << *it |
| 1210 | << "\n"; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1211 | it = _configurations.erase(it); |
| 1212 | continue; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1213 | } |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1214 | else if ((*findProbe).type() != nlohmann::json::value_t::array) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1215 | { |
| 1216 | probeCommand = nlohmann::json::array(); |
| 1217 | probeCommand.push_back(*findProbe); |
| 1218 | } |
| 1219 | else |
| 1220 | { |
| 1221 | probeCommand = *findProbe; |
| 1222 | } |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1223 | |
| 1224 | if (findName == it->end()) |
| 1225 | { |
| 1226 | std::cerr << "configuration file missing name:\n " << *it |
| 1227 | << "\n"; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1228 | it = _configurations.erase(it); |
| 1229 | continue; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1230 | } |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1231 | std::string probeName = *findName; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1232 | |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1233 | if (std::find(PASSED_PROBES.begin(), PASSED_PROBES.end(), |
| 1234 | probeName) != PASSED_PROBES.end()) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1235 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1236 | it = _configurations.erase(it); |
| 1237 | continue; |
| 1238 | } |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1239 | nlohmann::json* recordPtr = &(*it); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1240 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1241 | // store reference to this to children to makes sure we don't get |
| 1242 | // destroyed too early |
| 1243 | auto thisRef = shared_from_this(); |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1244 | auto p = std::make_shared< |
| 1245 | PerformProbe>(probeCommand, [&, recordPtr, probeName, thisRef]( |
| 1246 | FoundDeviceT& foundDevices) { |
| 1247 | _passed = true; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1248 | |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1249 | PASSED_PROBES.push_back(probeName); |
| 1250 | size_t foundDeviceIdx = 1; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1251 | |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1252 | for (auto& foundDevice : foundDevices) |
| 1253 | { |
| 1254 | nlohmann::json record = *recordPtr; |
| 1255 | std::string recordName; |
| 1256 | size_t hash = 0; |
| 1257 | if (foundDevice.size()) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1258 | { |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1259 | // use an array so alphabetical order from the |
| 1260 | // flat_map is maintained |
| 1261 | auto device = nlohmann::json::array(); |
| 1262 | for (auto& devPair : foundDevice) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1263 | { |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1264 | device.push_back(devPair.first); |
| 1265 | std::visit( |
| 1266 | [&device](auto&& v) { device.push_back(v); }, |
| 1267 | devPair.second); |
| 1268 | } |
| 1269 | hash = |
| 1270 | std::hash<std::string>{}(probeName + device.dump()); |
| 1271 | // hashes are hard to distinguish, use the |
| 1272 | // non-hashed version if we want debug |
| 1273 | if constexpr (DEBUG) |
| 1274 | { |
| 1275 | recordName = probeName + device.dump(); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1276 | } |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1277 | else |
| 1278 | { |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1279 | recordName = std::to_string(hash); |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1280 | } |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1281 | } |
| 1282 | else |
| 1283 | { |
| 1284 | recordName = probeName; |
| 1285 | } |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1286 | |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1287 | auto fromLastJson = lastJson.find(recordName); |
| 1288 | if (fromLastJson != lastJson.end()) |
| 1289 | { |
| 1290 | // keep user changes |
| 1291 | _systemConfiguration[recordName] = *fromLastJson; |
| 1292 | continue; |
| 1293 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1294 | |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1295 | // insert into configuration temporarily to be able to |
| 1296 | // reference ourselves |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1297 | |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1298 | _systemConfiguration[recordName] = record; |
| 1299 | |
| 1300 | for (auto keyPair = record.begin(); keyPair != record.end(); |
| 1301 | keyPair++) |
| 1302 | { |
| 1303 | templateCharReplace(keyPair, foundDevice, |
| 1304 | foundDeviceIdx); |
| 1305 | } |
| 1306 | |
| 1307 | auto findExpose = record.find("Exposes"); |
| 1308 | if (findExpose == record.end()) |
| 1309 | { |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1310 | _systemConfiguration[recordName] = record; |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1311 | logDeviceAdded(record); |
| 1312 | foundDeviceIdx++; |
| 1313 | continue; |
| 1314 | } |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1315 | |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1316 | for (auto& expose : *findExpose) |
| 1317 | { |
| 1318 | for (auto keyPair = expose.begin(); |
| 1319 | keyPair != expose.end(); keyPair++) |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1320 | { |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1321 | |
| 1322 | templateCharReplace(keyPair, foundDevice, |
| 1323 | foundDeviceIdx); |
| 1324 | |
| 1325 | // special case bind |
| 1326 | if (boost::starts_with(keyPair.key(), "Bind")) |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1327 | { |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1328 | if (keyPair.value().type() != |
| 1329 | nlohmann::json::value_t::string) |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1330 | { |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1331 | std::cerr << "bind_ value must be of " |
| 1332 | "type string " |
| 1333 | << keyPair.key() << "\n"; |
| 1334 | continue; |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1335 | } |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1336 | bool foundBind = false; |
| 1337 | std::string bind = |
| 1338 | keyPair.key().substr(sizeof("Bind") - 1); |
| 1339 | |
| 1340 | for (auto& configurationPair : |
| 1341 | _systemConfiguration.items()) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1342 | { |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1343 | |
| 1344 | auto configListFind = |
| 1345 | configurationPair.value().find( |
| 1346 | "Exposes"); |
| 1347 | |
| 1348 | if (configListFind == |
| 1349 | configurationPair.value().end() || |
| 1350 | configListFind->type() != |
| 1351 | nlohmann::json::value_t::array) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1352 | { |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1353 | continue; |
| 1354 | } |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1355 | for (auto& exposedObject : *configListFind) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1356 | { |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1357 | std::string foundObjectName = |
| 1358 | (exposedObject)["Name"]; |
| 1359 | if (boost::iequals( |
| 1360 | foundObjectName, |
| 1361 | keyPair.value() |
| 1362 | .get<std::string>())) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1363 | { |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1364 | exposedObject["Status"] = "okay"; |
| 1365 | expose[bind] = exposedObject; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1366 | |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1367 | foundBind = true; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1368 | break; |
| 1369 | } |
| 1370 | } |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1371 | if (foundBind) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1372 | { |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1373 | break; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1374 | } |
| 1375 | } |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1376 | if (!foundBind) |
| 1377 | { |
| 1378 | std::cerr << "configuration file " |
| 1379 | "dependency error, " |
| 1380 | "could not find bind " |
| 1381 | << keyPair.value() << "\n"; |
| 1382 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1383 | } |
| 1384 | } |
| 1385 | } |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1386 | // overwrite ourselves with cleaned up version |
| 1387 | _systemConfiguration[recordName] = record; |
| 1388 | |
| 1389 | logDeviceAdded(record); |
| 1390 | |
| 1391 | foundDeviceIdx++; |
| 1392 | } |
| 1393 | }); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1394 | p->run(); |
| 1395 | it++; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1396 | } |
| 1397 | } |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1398 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1399 | ~PerformScan() |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1400 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1401 | if (_passed) |
| 1402 | { |
| 1403 | auto nextScan = std::make_shared<PerformScan>( |
| 1404 | _systemConfiguration, _configurations, std::move(_callback)); |
| 1405 | nextScan->run(); |
| 1406 | } |
| 1407 | else |
| 1408 | { |
| 1409 | _callback(); |
| 1410 | } |
| 1411 | } |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1412 | nlohmann::json& _systemConfiguration; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1413 | std::list<nlohmann::json> _configurations; |
| 1414 | std::function<void(void)> _callback; |
| 1415 | std::vector<std::shared_ptr<PerformProbe>> _probes; |
| 1416 | bool _passed = false; |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1417 | bool powerWasOn = isPowerOn(); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1418 | }; |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 1419 | |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1420 | void startRemovedTimer(boost::asio::deadline_timer& timer, |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1421 | nlohmann::json& systemConfiguration) |
| 1422 | { |
| 1423 | static bool scannedPowerOff = false; |
| 1424 | static bool scannedPowerOn = false; |
| 1425 | |
James Feist | fb00f39 | 2019-06-25 14:16:48 -0700 | [diff] [blame] | 1426 | if (systemConfiguration.empty() || lastJson.empty()) |
| 1427 | { |
| 1428 | return; // not ready yet |
| 1429 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1430 | if (scannedPowerOn) |
| 1431 | { |
| 1432 | return; |
| 1433 | } |
| 1434 | |
| 1435 | if (!isPowerOn() && scannedPowerOff) |
| 1436 | { |
| 1437 | return; |
| 1438 | } |
| 1439 | |
| 1440 | timer.expires_from_now(boost::posix_time::seconds(10)); |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 1441 | timer.async_wait( |
| 1442 | [&systemConfiguration](const boost::system::error_code& ec) { |
| 1443 | if (ec == boost::asio::error::operation_aborted) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1444 | { |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 1445 | // we were cancelled |
| 1446 | return; |
| 1447 | } |
| 1448 | |
| 1449 | bool powerOff = !isPowerOn(); |
| 1450 | for (const auto& item : lastJson.items()) |
| 1451 | { |
| 1452 | if (systemConfiguration.find(item.key()) == |
| 1453 | systemConfiguration.end()) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1454 | { |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 1455 | bool isDetectedPowerOn = false; |
| 1456 | auto powerState = item.value().find("PowerState"); |
| 1457 | if (powerState != item.value().end()) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1458 | { |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 1459 | auto ptr = powerState->get_ptr<const std::string*>(); |
| 1460 | if (ptr) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1461 | { |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 1462 | if (*ptr == "On" || *ptr == "BiosPost") |
| 1463 | { |
| 1464 | isDetectedPowerOn = true; |
| 1465 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1466 | } |
| 1467 | } |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 1468 | if (powerOff && isDetectedPowerOn) |
| 1469 | { |
| 1470 | // power not on yet, don't know if it's there or not |
| 1471 | continue; |
| 1472 | } |
| 1473 | if (!powerOff && scannedPowerOff && isDetectedPowerOn) |
| 1474 | { |
| 1475 | // already logged it when power was off |
| 1476 | continue; |
| 1477 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1478 | |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 1479 | logDeviceRemoved(item.value()); |
| 1480 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1481 | } |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 1482 | scannedPowerOff = true; |
| 1483 | if (!powerOff) |
| 1484 | { |
| 1485 | scannedPowerOn = true; |
| 1486 | } |
| 1487 | }); |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1488 | } |
| 1489 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1490 | // main properties changed entry |
| 1491 | void propertiesChangedCallback( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1492 | boost::asio::io_service& io, |
| 1493 | std::vector<sdbusplus::bus::match::match>& dbusMatches, |
| 1494 | nlohmann::json& systemConfiguration, |
| 1495 | sdbusplus::asio::object_server& objServer) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1496 | { |
| 1497 | static boost::asio::deadline_timer timer(io); |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1498 | static bool timerRunning; |
| 1499 | |
| 1500 | timerRunning = true; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1501 | timer.expires_from_now(boost::posix_time::seconds(1)); |
| 1502 | |
| 1503 | // setup an async wait as we normally get flooded with new requests |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1504 | timer.async_wait([&](const boost::system::error_code& ec) { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1505 | if (ec == boost::asio::error::operation_aborted) |
| 1506 | { |
| 1507 | // we were cancelled |
| 1508 | return; |
| 1509 | } |
| 1510 | else if (ec) |
| 1511 | { |
| 1512 | std::cerr << "async wait error " << ec << "\n"; |
| 1513 | return; |
| 1514 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1515 | timerRunning = false; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1516 | |
| 1517 | nlohmann::json oldConfiguration = systemConfiguration; |
| 1518 | DBUS_PROBE_OBJECTS.clear(); |
| 1519 | |
| 1520 | std::list<nlohmann::json> configurations; |
| 1521 | if (!findJsonFiles(configurations)) |
| 1522 | { |
| 1523 | std::cerr << "cannot find json files\n"; |
| 1524 | return; |
| 1525 | } |
| 1526 | |
| 1527 | auto perfScan = std::make_shared<PerformScan>( |
| 1528 | systemConfiguration, configurations, [&, oldConfiguration]() { |
| 1529 | nlohmann::json newConfiguration = systemConfiguration; |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1530 | for (auto it = newConfiguration.begin(); |
| 1531 | it != newConfiguration.end();) |
| 1532 | { |
| 1533 | auto findKey = oldConfiguration.find(it.key()); |
| 1534 | if (findKey != oldConfiguration.end()) |
| 1535 | { |
| 1536 | it = newConfiguration.erase(it); |
| 1537 | } |
| 1538 | else |
| 1539 | { |
| 1540 | it++; |
| 1541 | } |
| 1542 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1543 | registerCallbacks(io, dbusMatches, systemConfiguration, |
| 1544 | objServer); |
| 1545 | io.post([&, newConfiguration]() { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1546 | loadOverlays(newConfiguration); |
James Feist | ce4367c | 2018-10-16 09:19:57 -0700 | [diff] [blame] | 1547 | |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 1548 | io.post([&]() { |
| 1549 | if (!writeJsonFiles(systemConfiguration)) |
| 1550 | { |
| 1551 | std::cerr << "Error writing json files\n"; |
| 1552 | } |
| 1553 | }); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1554 | io.post([&, newConfiguration]() { |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 1555 | postToDbus(newConfiguration, systemConfiguration, |
| 1556 | objServer); |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1557 | if (!timerRunning) |
| 1558 | { |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1559 | startRemovedTimer(timer, systemConfiguration); |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1560 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1561 | }); |
| 1562 | }); |
| 1563 | }); |
| 1564 | perfScan->run(); |
| 1565 | }); |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1566 | } |
| 1567 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1568 | void registerCallbacks(boost::asio::io_service& io, |
| 1569 | std::vector<sdbusplus::bus::match::match>& dbusMatches, |
| 1570 | nlohmann::json& systemConfiguration, |
| 1571 | sdbusplus::asio::object_server& objServer) |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1572 | { |
| 1573 | static boost::container::flat_set<std::string> watchedObjects; |
| 1574 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1575 | for (const auto& objectMap : DBUS_PROBE_OBJECTS) |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1576 | { |
| 1577 | auto findObject = watchedObjects.find(objectMap.first); |
| 1578 | if (findObject != watchedObjects.end()) |
| 1579 | { |
| 1580 | continue; |
| 1581 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1582 | std::function<void(sdbusplus::message::message & message)> |
| 1583 | eventHandler = |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1584 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1585 | [&](sdbusplus::message::message&) { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1586 | propertiesChangedCallback(io, dbusMatches, |
| 1587 | systemConfiguration, objServer); |
| 1588 | }; |
| 1589 | |
| 1590 | sdbusplus::bus::match::match match( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1591 | static_cast<sdbusplus::bus::bus&>(*SYSTEM_BUS), |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1592 | "type='signal',member='PropertiesChanged',arg0='" + |
| 1593 | objectMap.first + "'", |
| 1594 | eventHandler); |
| 1595 | dbusMatches.emplace_back(std::move(match)); |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1596 | } |
| 1597 | } |
| 1598 | |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1599 | int main() |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1600 | { |
| 1601 | // setup connection to dbus |
| 1602 | boost::asio::io_service io; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1603 | SYSTEM_BUS = std::make_shared<sdbusplus::asio::connection>(io); |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1604 | SYSTEM_BUS->request_name("xyz.openbmc_project.EntityManager"); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1605 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1606 | sdbusplus::asio::object_server objServer(SYSTEM_BUS); |
James Feist | fd1264a | 2018-05-03 12:10:00 -0700 | [diff] [blame] | 1607 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1608 | std::shared_ptr<sdbusplus::asio::dbus_interface> entityIface = |
| 1609 | objServer.add_interface("/xyz/openbmc_project/EntityManager", |
| 1610 | "xyz.openbmc_project.EntityManager"); |
James Feist | fd1264a | 2018-05-03 12:10:00 -0700 | [diff] [blame] | 1611 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1612 | std::shared_ptr<sdbusplus::asio::dbus_interface> inventoryIface = |
| 1613 | objServer.add_interface("/xyz/openbmc_project/inventory", |
| 1614 | "xyz.openbmc_project.Inventory.Manager"); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1615 | |
| 1616 | // to keep reference to the match / filter objects so they don't get |
| 1617 | // destroyed |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1618 | std::vector<sdbusplus::bus::match::match> dbusMatches; |
| 1619 | |
| 1620 | nlohmann::json systemConfiguration = nlohmann::json::object(); |
| 1621 | |
| 1622 | inventoryIface->register_method( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1623 | "Notify", |
| 1624 | [](const boost::container::flat_map< |
| 1625 | std::string, |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1626 | boost::container::flat_map<std::string, BasicVariantType>>&) { |
| 1627 | return; |
| 1628 | }); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1629 | inventoryIface->initialize(); |
| 1630 | |
| 1631 | io.post([&]() { |
James Feist | ce4367c | 2018-10-16 09:19:57 -0700 | [diff] [blame] | 1632 | #if OVERLAYS |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1633 | unloadAllOverlays(); |
James Feist | ce4367c | 2018-10-16 09:19:57 -0700 | [diff] [blame] | 1634 | #endif |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1635 | propertiesChangedCallback(io, dbusMatches, systemConfiguration, |
| 1636 | objServer); |
| 1637 | }); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1638 | |
James Feist | fd1264a | 2018-05-03 12:10:00 -0700 | [diff] [blame] | 1639 | entityIface->register_method("ReScan", [&]() { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1640 | propertiesChangedCallback(io, dbusMatches, systemConfiguration, |
| 1641 | objServer); |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1642 | }); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1643 | entityIface->initialize(); |
| 1644 | |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1645 | if (fwVersionIsSame()) |
| 1646 | { |
| 1647 | if (std::filesystem::is_regular_file(currentConfiguration)) |
| 1648 | { |
| 1649 | // this file could just be deleted, but it's nice for debug |
| 1650 | std::filesystem::create_directory(tempConfigDir); |
| 1651 | std::filesystem::remove(lastConfiguration); |
| 1652 | std::filesystem::copy(currentConfiguration, lastConfiguration); |
| 1653 | std::filesystem::remove(currentConfiguration); |
| 1654 | |
| 1655 | std::ifstream jsonStream(lastConfiguration); |
| 1656 | if (jsonStream.good()) |
| 1657 | { |
| 1658 | auto data = nlohmann::json::parse(jsonStream, nullptr, false); |
| 1659 | if (data.is_discarded()) |
| 1660 | { |
| 1661 | std::cerr << "syntax error in " << lastConfiguration |
| 1662 | << "\n"; |
| 1663 | } |
| 1664 | else |
| 1665 | { |
| 1666 | lastJson = std::move(data); |
| 1667 | } |
| 1668 | } |
| 1669 | else |
| 1670 | { |
| 1671 | std::cerr << "unable to open " << lastConfiguration << "\n"; |
| 1672 | } |
| 1673 | } |
| 1674 | } |
| 1675 | else |
| 1676 | { |
| 1677 | // not an error, just logging at this level to make it in the journal |
| 1678 | std::cerr << "Clearing previous configuration\n"; |
| 1679 | std::filesystem::remove(currentConfiguration); |
| 1680 | } |
| 1681 | |
| 1682 | // some boards only show up after power is on, we want to not say they are |
| 1683 | // removed until the same state happens |
| 1684 | setupPowerMatch(SYSTEM_BUS); |
| 1685 | |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1686 | io.run(); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1687 | |
| 1688 | return 0; |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1689 | } |