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 | |
| 17 | #include <Utils.hpp> |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 18 | #include <Overlay.hpp> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 19 | #include <nlohmann/json.hpp> |
| 20 | #include <fstream> |
| 21 | #include <regex> |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 22 | #include <iostream> |
| 23 | #include <sdbusplus/asio/connection.hpp> |
| 24 | #include <sdbusplus/asio/object_server.hpp> |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 25 | #include <boost/algorithm/string/case_conv.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> |
| 28 | #include <boost/variant/apply_visitor.hpp> |
| 29 | #include <boost/lexical_cast.hpp> |
| 30 | #include <boost/container/flat_map.hpp> |
| 31 | #include <boost/container/flat_set.hpp> |
| 32 | #include <VariantVisitors.hpp> |
James Feist | 7b7e4e8 | 2018-01-24 14:56:00 -0800 | [diff] [blame] | 33 | #include <experimental/filesystem> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 34 | |
| 35 | constexpr const char *OUTPUT_DIR = "/var/configuration/"; |
| 36 | constexpr const char *CONFIGURATION_DIR = "/usr/share/configurations"; |
| 37 | constexpr const char *TEMPLATE_CHAR = "$"; |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 38 | constexpr const size_t PROPERTIES_CHANGED_UNTIL_FLUSH_COUNT = 20; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 39 | constexpr const int32_t MAX_MAPPER_DEPTH = 0; |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 40 | constexpr const size_t SLEEP_AFTER_PROPERTIES_CHANGE_SECONDS = 5; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 41 | |
| 42 | namespace fs = std::experimental::filesystem; |
| 43 | struct cmp_str |
| 44 | { |
| 45 | bool operator()(const char *a, const char *b) const |
| 46 | { |
| 47 | return std::strcmp(a, b) < 0; |
| 48 | } |
| 49 | }; |
| 50 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 51 | struct PerformProbe; |
| 52 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 53 | // underscore T for collison with dbus c api |
| 54 | enum class probe_type_codes |
| 55 | { |
| 56 | FALSE_T, |
| 57 | TRUE_T, |
| 58 | AND, |
| 59 | OR, |
James Feist | 6bd2a02 | 2018-03-13 12:30:58 -0700 | [diff] [blame] | 60 | FOUND, |
| 61 | MATCH_ONE |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 62 | }; |
| 63 | const static boost::container::flat_map<const char *, probe_type_codes, cmp_str> |
| 64 | PROBE_TYPES{{{"FALSE", probe_type_codes::FALSE_T}, |
| 65 | {"TRUE", probe_type_codes::TRUE_T}, |
| 66 | {"AND", probe_type_codes::AND}, |
| 67 | {"OR", probe_type_codes::OR}, |
James Feist | 6bd2a02 | 2018-03-13 12:30:58 -0700 | [diff] [blame] | 68 | {"FOUND", probe_type_codes::FOUND}, |
| 69 | {"MATCH_ONE", probe_type_codes::MATCH_ONE}}}; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 70 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 71 | static constexpr std::array<const char *, 1> SETTABLE_INTERFACES = { |
| 72 | "thresholds"}; |
| 73 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 74 | using BasicVariantType = |
| 75 | sdbusplus::message::variant<std::string, int64_t, uint64_t, double, int32_t, |
| 76 | uint32_t, int16_t, uint16_t, uint8_t, bool>; |
| 77 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 78 | using GetSubTreeType = std::vector< |
| 79 | std::pair<std::string, |
| 80 | std::vector<std::pair<std::string, std::vector<std::string>>>>>; |
| 81 | |
| 82 | using ManagedObjectType = boost::container::flat_map< |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 83 | sdbusplus::message::object_path, |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 84 | boost::container::flat_map< |
| 85 | std::string, |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 86 | boost::container::flat_map<std::string, BasicVariantType>>>; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 87 | |
| 88 | boost::container::flat_map< |
| 89 | std::string, |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 90 | std::vector<boost::container::flat_map<std::string, BasicVariantType>>> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 91 | DBUS_PROBE_OBJECTS; |
| 92 | std::vector<std::string> PASSED_PROBES; |
| 93 | |
| 94 | // todo: pass this through nicer |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 95 | std::shared_ptr<sdbusplus::asio::connection> SYSTEM_BUS; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 96 | |
James Feist | a675024 | 2018-07-16 14:12:27 -0700 | [diff] [blame] | 97 | const std::regex ILLEGAL_DBUS_REGEX("[^A-Za-z0-9_.]"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 98 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 99 | void registerCallbacks(boost::asio::io_service &io, |
| 100 | std::vector<sdbusplus::bus::match::match> &dbusMatches, |
| 101 | nlohmann::json &systemConfiguration, |
| 102 | sdbusplus::asio::object_server &objServer); |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 103 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 104 | // calls the mapper to find all exposed objects of an interface type |
| 105 | // and creates a vector<flat_map> that contains all the key value pairs |
| 106 | // getManagedObjects |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 107 | void findDbusObjects(std::shared_ptr<PerformProbe> probe, |
| 108 | std::shared_ptr<sdbusplus::asio::connection> connection, |
| 109 | std::string &interface) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 110 | { |
James Feist | 494155a | 2018-03-14 16:23:24 -0700 | [diff] [blame] | 111 | // todo: this is only static because the mapper is unreliable as of today |
| 112 | static boost::container::flat_map<std::string, |
| 113 | boost::container::flat_set<std::string>> |
| 114 | connections; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 115 | |
| 116 | // store reference to pending callbacks so we don't overwhelm services |
| 117 | static boost::container::flat_map< |
| 118 | std::string, std::vector<std::shared_ptr<PerformProbe>>> |
| 119 | pendingProbes; |
| 120 | |
| 121 | if (DBUS_PROBE_OBJECTS[interface].size()) |
| 122 | { |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | // add shared_ptr to vector of Probes waiting for callback from a specific |
| 127 | // interface to keep alive while waiting for response |
| 128 | std::array<const char *, 1> objects = {interface.c_str()}; |
| 129 | std::vector<std::shared_ptr<PerformProbe>> &pending = |
| 130 | pendingProbes[interface]; |
| 131 | auto iter = pending.emplace(pending.end(), probe); |
| 132 | // only allow first call to run to not overwhelm processes |
| 133 | if (iter != pending.begin()) |
| 134 | { |
| 135 | return; |
| 136 | } |
| 137 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 138 | // find all connections in the mapper that expose a specific type |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 139 | connection->async_method_call( |
| 140 | [&, connection, interface](boost::system::error_code &ec, |
| 141 | const GetSubTreeType &interfaceSubtree) { |
| 142 | auto &interfaceConnections = connections[interface]; |
| 143 | if (ec) |
James Feist | 494155a | 2018-03-14 16:23:24 -0700 | [diff] [blame] | 144 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 145 | std::cerr |
| 146 | << "Error communicating to mapper, using cached data if " |
| 147 | "available\n"; |
| 148 | if (interfaceConnections.empty()) |
| 149 | { |
James Feist | f26422f | 2018-07-09 10:26:47 -0700 | [diff] [blame] | 150 | // if we can't get the mapper data on the first run, |
| 151 | // something is very wrong |
| 152 | std::exit(EXIT_FAILURE); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 153 | } |
James Feist | 494155a | 2018-03-14 16:23:24 -0700 | [diff] [blame] | 154 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 155 | else |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 156 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 157 | for (auto &object : interfaceSubtree) |
| 158 | { |
| 159 | for (auto &connPair : object.second) |
| 160 | { |
| 161 | auto insertPair = |
| 162 | interfaceConnections.insert(connPair.first); |
| 163 | } |
| 164 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 165 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 166 | // get managed objects for all interfaces |
| 167 | for (const auto &conn : interfaceConnections) |
| 168 | { |
| 169 | connection->async_method_call( |
| 170 | [&, conn, |
| 171 | interface](boost::system::error_code &ec, |
| 172 | const ManagedObjectType &managedInterface) { |
| 173 | if (ec) |
| 174 | { |
| 175 | std::cerr |
| 176 | << "error getting managed object for device " |
| 177 | << conn << "\n"; |
| 178 | pendingProbes[interface].clear(); |
| 179 | return; |
| 180 | } |
| 181 | for (auto &interfaceManagedObj : managedInterface) |
| 182 | { |
| 183 | auto ifaceObjFind = |
| 184 | interfaceManagedObj.second.find(interface); |
| 185 | if (ifaceObjFind != |
| 186 | interfaceManagedObj.second.end()) |
| 187 | { |
| 188 | std::vector<boost::container::flat_map< |
| 189 | std::string, BasicVariantType>> |
| 190 | &dbusObject = DBUS_PROBE_OBJECTS[interface]; |
| 191 | dbusObject.emplace_back(ifaceObjFind->second); |
| 192 | } |
| 193 | } |
| 194 | pendingProbes[interface].clear(); |
| 195 | }, |
| 196 | conn.c_str(), "/", "org.freedesktop.DBus.ObjectManager", |
| 197 | "GetManagedObjects"); |
| 198 | } |
| 199 | }, |
| 200 | "xyz.openbmc_project.ObjectMapper", |
| 201 | "/xyz/openbmc_project/object_mapper", |
| 202 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", "", MAX_MAPPER_DEPTH, |
| 203 | objects); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 204 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 205 | // probes dbus interface dictionary for a key with a value that matches a regex |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 206 | bool probeDbus( |
| 207 | const std::string &interface, |
| 208 | const std::map<std::string, nlohmann::json> &matches, |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 209 | std::vector<boost::container::flat_map<std::string, BasicVariantType>> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 210 | &devices, |
| 211 | bool &foundProbe) |
| 212 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 213 | std::vector<boost::container::flat_map<std::string, BasicVariantType>> |
| 214 | &dbusObject = DBUS_PROBE_OBJECTS[interface]; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 215 | if (dbusObject.empty()) |
| 216 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 217 | foundProbe = false; |
| 218 | return false; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 219 | } |
| 220 | foundProbe = true; |
| 221 | |
| 222 | bool foundMatch = false; |
| 223 | for (auto &device : dbusObject) |
| 224 | { |
| 225 | bool deviceMatches = true; |
| 226 | for (auto &match : matches) |
| 227 | { |
| 228 | auto deviceValue = device.find(match.first); |
| 229 | if (deviceValue != device.end()) |
| 230 | { |
| 231 | switch (match.second.type()) |
| 232 | { |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 233 | case nlohmann::json::value_t::string: |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 234 | { |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 235 | std::regex search(match.second.get<std::string>()); |
| 236 | std::smatch match; |
| 237 | |
| 238 | // convert value to string respresentation |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 239 | std::string probeValue = mapbox::util::apply_visitor( |
| 240 | VariantToStringVisitor(), deviceValue->second); |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 241 | if (!std::regex_search(probeValue, match, search)) |
| 242 | { |
| 243 | deviceMatches = false; |
| 244 | break; |
| 245 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 246 | break; |
| 247 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 248 | case nlohmann::json::value_t::boolean: |
| 249 | case nlohmann::json::value_t::number_unsigned: |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 250 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 251 | unsigned int probeValue = mapbox::util::apply_visitor( |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 252 | VariantToUnsignedIntVisitor(), deviceValue->second); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 253 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 254 | if (probeValue != match.second.get<unsigned int>()) |
| 255 | { |
| 256 | deviceMatches = false; |
| 257 | } |
| 258 | break; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 259 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 260 | case nlohmann::json::value_t::number_integer: |
| 261 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 262 | int probeValue = mapbox::util::apply_visitor( |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 263 | VariantToIntVisitor(), deviceValue->second); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 264 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 265 | if (probeValue != match.second.get<int>()) |
| 266 | { |
| 267 | deviceMatches = false; |
| 268 | } |
| 269 | break; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 270 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 271 | case nlohmann::json::value_t::number_float: |
| 272 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 273 | float probeValue = mapbox::util::apply_visitor( |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 274 | VariantToFloatVisitor(), deviceValue->second); |
| 275 | |
| 276 | if (probeValue != match.second.get<float>()) |
| 277 | { |
| 278 | deviceMatches = false; |
| 279 | } |
| 280 | break; |
| 281 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | else |
| 285 | { |
| 286 | deviceMatches = false; |
| 287 | break; |
| 288 | } |
| 289 | } |
| 290 | if (deviceMatches) |
| 291 | { |
| 292 | devices.emplace_back( |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 293 | boost::container::flat_map<std::string, BasicVariantType>( |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 294 | device)); |
| 295 | foundMatch = true; |
| 296 | deviceMatches = false; // for next iteration |
| 297 | } |
| 298 | } |
| 299 | return foundMatch; |
| 300 | } |
| 301 | |
| 302 | // default probe entry point, iterates a list looking for specific types to |
| 303 | // call specific probe functions |
| 304 | bool probe( |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 305 | const std::vector<std::string> &probeCommand, |
| 306 | std::vector<boost::container::flat_map<std::string, BasicVariantType>> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 307 | &foundDevs) |
| 308 | { |
| 309 | const static std::regex command(R"(\((.*)\))"); |
| 310 | std::smatch match; |
| 311 | bool ret = false; |
James Feist | 6bd2a02 | 2018-03-13 12:30:58 -0700 | [diff] [blame] | 312 | bool matchOne = false; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 313 | bool cur = true; |
| 314 | probe_type_codes lastCommand = probe_type_codes::FALSE_T; |
| 315 | |
| 316 | for (auto &probe : probeCommand) |
| 317 | { |
| 318 | bool foundProbe = false; |
| 319 | boost::container::flat_map<const char *, probe_type_codes, |
| 320 | cmp_str>::const_iterator probeType; |
| 321 | |
| 322 | for (probeType = PROBE_TYPES.begin(); probeType != PROBE_TYPES.end(); |
| 323 | probeType++) |
| 324 | { |
| 325 | if (probe.find(probeType->first) != std::string::npos) |
| 326 | { |
| 327 | foundProbe = true; |
| 328 | break; |
| 329 | } |
| 330 | } |
| 331 | if (foundProbe) |
| 332 | { |
| 333 | switch (probeType->second) |
| 334 | { |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 335 | case probe_type_codes::FALSE_T: |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 336 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 337 | return false; |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 338 | break; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 339 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 340 | case probe_type_codes::TRUE_T: |
| 341 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 342 | return true; |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 343 | break; |
| 344 | } |
| 345 | case probe_type_codes::MATCH_ONE: |
| 346 | { |
| 347 | // set current value to last, this probe type shouldn't |
| 348 | // affect the outcome |
| 349 | cur = ret; |
| 350 | matchOne = true; |
| 351 | break; |
| 352 | } |
| 353 | /*case probe_type_codes::AND: |
| 354 | break; |
| 355 | case probe_type_codes::OR: |
| 356 | break; |
| 357 | // these are no-ops until the last command switch |
| 358 | */ |
| 359 | case probe_type_codes::FOUND: |
| 360 | { |
| 361 | if (!std::regex_search(probe, match, command)) |
| 362 | { |
| 363 | std::cerr << "found probe sytax error " << probe |
| 364 | << "\n"; |
| 365 | return false; |
| 366 | } |
| 367 | std::string commandStr = *(match.begin() + 1); |
| 368 | boost::replace_all(commandStr, "'", ""); |
| 369 | cur = (std::find(PASSED_PROBES.begin(), PASSED_PROBES.end(), |
| 370 | commandStr) != PASSED_PROBES.end()); |
| 371 | break; |
| 372 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | // look on dbus for object |
| 376 | else |
| 377 | { |
| 378 | if (!std::regex_search(probe, match, command)) |
| 379 | { |
| 380 | std::cerr << "dbus probe sytax error " << probe << "\n"; |
| 381 | return false; |
| 382 | } |
| 383 | std::string commandStr = *(match.begin() + 1); |
| 384 | // convert single ticks and single slashes into legal json |
Jae Hyun Yoo | 3936e7a | 2018-03-23 17:26:16 -0700 | [diff] [blame] | 385 | boost::replace_all(commandStr, "'", "\""); |
James Feist | 3f8a278 | 2018-02-12 09:24:42 -0800 | [diff] [blame] | 386 | boost::replace_all(commandStr, R"(\)", R"(\\)"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 387 | auto json = nlohmann::json::parse(commandStr, nullptr, false); |
| 388 | if (json.is_discarded()) |
| 389 | { |
| 390 | std::cerr << "dbus command sytax error " << commandStr << "\n"; |
| 391 | return false; |
| 392 | } |
| 393 | // we can match any (string, variant) property. (string, string) |
| 394 | // does a regex |
| 395 | std::map<std::string, nlohmann::json> dbusProbeMap = |
| 396 | json.get<std::map<std::string, nlohmann::json>>(); |
| 397 | auto findStart = probe.find("("); |
| 398 | if (findStart == std::string::npos) |
| 399 | { |
| 400 | return false; |
| 401 | } |
| 402 | std::string probeInterface = probe.substr(0, findStart); |
| 403 | cur = |
| 404 | probeDbus(probeInterface, dbusProbeMap, foundDevs, foundProbe); |
| 405 | } |
| 406 | |
| 407 | // some functions like AND and OR only take affect after the |
| 408 | // fact |
| 409 | switch (lastCommand) |
| 410 | { |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 411 | case probe_type_codes::AND: |
| 412 | ret = cur && ret; |
| 413 | break; |
| 414 | case probe_type_codes::OR: |
| 415 | ret = cur || ret; |
| 416 | break; |
| 417 | default: |
| 418 | ret = cur; |
| 419 | break; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 420 | } |
| 421 | lastCommand = probeType != PROBE_TYPES.end() |
| 422 | ? probeType->second |
| 423 | : probe_type_codes::FALSE_T; |
| 424 | |
| 425 | if (!foundProbe) |
| 426 | { |
| 427 | std::cerr << "Illegal probe type " << probe << "\n"; |
| 428 | return false; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | // probe passed, but empty device |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 433 | if (ret && foundDevs.size() == 0) |
| 434 | { |
| 435 | foundDevs.emplace_back( |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 436 | boost::container::flat_map<std::string, BasicVariantType>()); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 437 | } |
James Feist | 6bd2a02 | 2018-03-13 12:30:58 -0700 | [diff] [blame] | 438 | if (matchOne && foundDevs.size() > 1) |
| 439 | { |
| 440 | foundDevs.erase(foundDevs.begin() + 1, foundDevs.end()); |
| 441 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 442 | return ret; |
| 443 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 444 | // this class finds the needed dbus fields and on destruction runs the probe |
| 445 | struct PerformProbe : std::enable_shared_from_this<PerformProbe> |
| 446 | { |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 447 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 448 | PerformProbe( |
| 449 | const std::vector<std::string> &probeCommand, |
| 450 | std::function<void(std::vector<boost::container::flat_map< |
| 451 | std::string, BasicVariantType>> &)> &&callback) : |
| 452 | _probeCommand(probeCommand), |
| 453 | _callback(std::move(callback)) |
| 454 | { |
| 455 | } |
| 456 | ~PerformProbe() |
| 457 | { |
| 458 | if (probe(_probeCommand, _foundDevs)) |
| 459 | { |
| 460 | _callback(_foundDevs); |
| 461 | } |
| 462 | } |
| 463 | void run() |
| 464 | { |
| 465 | // parse out dbus probes by discarding other probe types |
| 466 | boost::container::flat_map<const char *, probe_type_codes, |
| 467 | cmp_str>::const_iterator probeType; |
| 468 | |
| 469 | std::vector<std::string> dbusProbes; |
| 470 | for (std::string &probe : _probeCommand) |
| 471 | { |
| 472 | bool found = false; |
| 473 | boost::container::flat_map<const char *, probe_type_codes, |
| 474 | cmp_str>::const_iterator probeType; |
| 475 | for (probeType = PROBE_TYPES.begin(); |
| 476 | probeType != PROBE_TYPES.end(); probeType++) |
| 477 | { |
| 478 | if (probe.find(probeType->first) != std::string::npos) |
| 479 | { |
| 480 | found = true; |
| 481 | break; |
| 482 | } |
| 483 | } |
| 484 | if (found) |
| 485 | { |
| 486 | continue; |
| 487 | } |
| 488 | // syntax requires probe before first open brace |
| 489 | auto findStart = probe.find("("); |
| 490 | std::string interface = probe.substr(0, findStart); |
| 491 | |
| 492 | findDbusObjects(shared_from_this(), SYSTEM_BUS, interface); |
| 493 | } |
| 494 | } |
| 495 | std::vector<std::string> _probeCommand; |
| 496 | std::function<void( |
| 497 | std::vector<boost::container::flat_map<std::string, BasicVariantType>> |
| 498 | &)> |
| 499 | _callback; |
| 500 | std::vector<boost::container::flat_map<std::string, BasicVariantType>> |
| 501 | _foundDevs; |
| 502 | }; |
| 503 | |
| 504 | // writes output files to persist data |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 505 | bool writeJsonFiles(const nlohmann::json &systemConfiguration) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 506 | { |
| 507 | std::experimental::filesystem::create_directory(OUTPUT_DIR); |
| 508 | std::ofstream output(std::string(OUTPUT_DIR) + "system.json"); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 509 | if (!output.good()) |
| 510 | { |
| 511 | return false; |
| 512 | } |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 513 | output << systemConfiguration.dump(4); |
| 514 | output.close(); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 515 | return true; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 516 | } |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 517 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 518 | // template function to add array as dbus property |
| 519 | template <typename PropertyType> |
| 520 | void addArrayToDbus(const std::string &name, const nlohmann::json &array, |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 521 | sdbusplus::asio::dbus_interface *iface, |
| 522 | sdbusplus::asio::PropertyPermission permission) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 523 | { |
| 524 | std::vector<PropertyType> values; |
| 525 | for (const auto &property : array) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 526 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 527 | auto ptr = property.get_ptr<const PropertyType *>(); |
| 528 | if (ptr != nullptr) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 529 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 530 | values.emplace_back(*ptr); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 531 | } |
| 532 | } |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 533 | // todo(james), currently there are no reason to persist arrays, get around |
| 534 | // to it if needed |
| 535 | |
| 536 | iface->register_property(name, values, permission); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 537 | } |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 538 | |
| 539 | template <typename JsonType> |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 540 | bool setJsonFromPointer(const std::string &ptrStr, const JsonType &value, |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 541 | nlohmann::json &systemConfiguration) |
| 542 | { |
| 543 | try |
| 544 | { |
| 545 | nlohmann::json::json_pointer ptr(ptrStr); |
| 546 | nlohmann::json &ref = systemConfiguration[ptr]; |
| 547 | ref = value; |
| 548 | return true; |
| 549 | } |
| 550 | catch (const std::out_of_range) |
| 551 | { |
| 552 | return false; |
| 553 | } |
| 554 | } |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 555 | |
| 556 | template <typename PropertyType> |
| 557 | void addProperty(const std::string &propertyName, const PropertyType &value, |
| 558 | sdbusplus::asio::dbus_interface *iface, |
| 559 | nlohmann::json &systemConfiguration, |
| 560 | const std::string &jsonPointerString, |
| 561 | sdbusplus::asio::PropertyPermission permission) |
| 562 | { |
| 563 | if (permission == sdbusplus::asio::PropertyPermission::readOnly) |
| 564 | { |
| 565 | iface->register_property(propertyName, value); |
| 566 | return; |
| 567 | } |
| 568 | iface->register_property( |
| 569 | propertyName, value, |
| 570 | [&systemConfiguration, |
| 571 | jsonPointerString{std::string(jsonPointerString)}]( |
| 572 | const PropertyType &newVal, 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 | |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 589 | // adds simple json types to interface's properties |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 590 | void populateInterfaceFromJson( |
| 591 | nlohmann::json &systemConfiguration, const std::string &jsonPointerPath, |
| 592 | sdbusplus::asio::dbus_interface *iface, nlohmann::json &dict, |
| 593 | sdbusplus::asio::object_server &objServer, |
| 594 | sdbusplus::asio::PropertyPermission permission = |
| 595 | sdbusplus::asio::PropertyPermission::readOnly) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 596 | { |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 597 | for (auto &dictPair : dict.items()) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 598 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 599 | auto type = dictPair.value().type(); |
| 600 | bool array = false; |
| 601 | if (dictPair.value().type() == nlohmann::json::value_t::array) |
| 602 | { |
| 603 | array = true; |
| 604 | if (!dictPair.value().size()) |
| 605 | { |
| 606 | continue; |
| 607 | } |
| 608 | type = dictPair.value()[0].type(); |
| 609 | bool isLegal = true; |
| 610 | for (const auto &arrayItem : dictPair.value()) |
| 611 | { |
| 612 | if (arrayItem.type() != type) |
| 613 | { |
| 614 | isLegal = false; |
| 615 | break; |
| 616 | } |
| 617 | } |
| 618 | if (!isLegal) |
| 619 | { |
| 620 | std::cerr << "dbus format error" << dictPair.value() << "\n"; |
| 621 | continue; |
| 622 | } |
| 623 | if (type == nlohmann::json::value_t::object) |
| 624 | { |
| 625 | continue; // handled elsewhere |
| 626 | } |
| 627 | } |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 628 | std::string key = jsonPointerPath + "/" + dictPair.key(); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 629 | if (permission == sdbusplus::asio::PropertyPermission::readWrite) |
| 630 | { |
| 631 | // all setable numbers are doubles as it is difficult to always |
| 632 | // create a configuration file with all whole numbers as decimals |
| 633 | // i.e. 1.0 |
| 634 | if (dictPair.value().is_number()) |
| 635 | { |
| 636 | type = nlohmann::json::value_t::number_float; |
| 637 | } |
| 638 | } |
| 639 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 640 | switch (type) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 641 | { |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 642 | case (nlohmann::json::value_t::boolean): |
| 643 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 644 | if (array) |
| 645 | { |
| 646 | // todo: array of bool isn't detected correctly by |
| 647 | // sdbusplus, change it to numbers |
| 648 | addArrayToDbus<uint64_t>(dictPair.key(), dictPair.value(), |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 649 | iface, permission); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 650 | } |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 651 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 652 | else |
| 653 | { |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 654 | addProperty(dictPair.key(), dictPair.value().get<bool>(), |
| 655 | iface, systemConfiguration, key, permission); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 656 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 657 | break; |
| 658 | } |
| 659 | case (nlohmann::json::value_t::number_integer): |
| 660 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 661 | if (array) |
| 662 | { |
| 663 | addArrayToDbus<int64_t>(dictPair.key(), dictPair.value(), |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 664 | iface, permission); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 665 | } |
| 666 | else |
| 667 | { |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 668 | addProperty(dictPair.key(), dictPair.value().get<int64_t>(), |
| 669 | iface, systemConfiguration, key, |
| 670 | sdbusplus::asio::PropertyPermission::readOnly); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 671 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 672 | break; |
| 673 | } |
| 674 | case (nlohmann::json::value_t::number_unsigned): |
| 675 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 676 | if (array) |
| 677 | { |
| 678 | addArrayToDbus<uint64_t>(dictPair.key(), dictPair.value(), |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 679 | iface, permission); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 680 | } |
| 681 | else |
| 682 | { |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 683 | addProperty(dictPair.key(), |
| 684 | dictPair.value().get<uint64_t>(), iface, |
| 685 | systemConfiguration, key, |
| 686 | sdbusplus::asio::PropertyPermission::readOnly); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 687 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 688 | break; |
| 689 | } |
| 690 | case (nlohmann::json::value_t::number_float): |
| 691 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 692 | if (array) |
| 693 | { |
| 694 | addArrayToDbus<double>(dictPair.key(), dictPair.value(), |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 695 | iface, permission); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 696 | } |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 697 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 698 | else |
| 699 | { |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 700 | addProperty(dictPair.key(), dictPair.value().get<double>(), |
| 701 | iface, systemConfiguration, key, permission); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 702 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 703 | break; |
| 704 | } |
| 705 | case (nlohmann::json::value_t::string): |
| 706 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 707 | if (array) |
| 708 | { |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 709 | addArrayToDbus<std::string>( |
| 710 | dictPair.key(), dictPair.value(), iface, permission); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 711 | } |
| 712 | else |
| 713 | { |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 714 | addProperty(dictPair.key(), |
| 715 | dictPair.value().get<std::string>(), iface, |
| 716 | systemConfiguration, key, permission); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 717 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 718 | break; |
| 719 | } |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 720 | } |
| 721 | } |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 722 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 723 | iface->initialize(); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 724 | } |
| 725 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 726 | void postToDbus(const nlohmann::json &newConfiguration, |
| 727 | nlohmann::json &systemConfiguration, |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 728 | sdbusplus::asio::object_server &objServer) |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 729 | |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 730 | { |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 731 | // iterate through boards |
| 732 | for (auto &boardPair : newConfiguration.items()) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 733 | { |
| 734 | std::string boardKey = boardPair.key(); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 735 | std::vector<std::string> path; |
| 736 | std::string jsonPointerPath = "/" + boardKey; |
| 737 | // loop through newConfiguration, but use values from system |
| 738 | // configuration to be able to modify via dbus later |
| 739 | auto boardValues = systemConfiguration[boardKey]; |
James Feist | d63d18a | 2018-07-19 15:23:45 -0700 | [diff] [blame] | 740 | auto findBoardType = boardValues.find("Type"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 741 | std::string boardType; |
| 742 | if (findBoardType != boardValues.end() && |
| 743 | findBoardType->type() == nlohmann::json::value_t::string) |
| 744 | { |
| 745 | boardType = findBoardType->get<std::string>(); |
| 746 | std::regex_replace(boardType.begin(), boardType.begin(), |
| 747 | boardType.end(), ILLEGAL_DBUS_REGEX, "_"); |
| 748 | } |
| 749 | else |
| 750 | { |
| 751 | std::cerr << "Unable to find type for " << boardKey |
| 752 | << " reverting to Chassis.\n"; |
| 753 | boardType = "Chassis"; |
| 754 | } |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 755 | std::string boardtypeLower = boost::algorithm::to_lower_copy(boardType); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 756 | |
| 757 | std::regex_replace(boardKey.begin(), boardKey.begin(), boardKey.end(), |
| 758 | ILLEGAL_DBUS_REGEX, "_"); |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 759 | std::string boardName = "/xyz/openbmc_project/inventory/system/" + |
| 760 | boardtypeLower + "/" + boardKey; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 761 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 762 | auto inventoryIface = objServer.add_interface( |
| 763 | boardName, "xyz.openbmc_project.Inventory.Item"); |
| 764 | auto boardIface = objServer.add_interface( |
| 765 | boardName, "xyz.openbmc_project.Inventory.Item." + boardType); |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 766 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 767 | populateInterfaceFromJson(systemConfiguration, jsonPointerPath, |
| 768 | boardIface.get(), boardValues, objServer); |
| 769 | jsonPointerPath += "/"; |
| 770 | // iterate through board properties |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 771 | for (auto &boardField : boardValues.items()) |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 772 | { |
| 773 | if (boardField.value().type() == nlohmann::json::value_t::object) |
| 774 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 775 | auto iface = |
| 776 | objServer.add_interface(boardName, boardField.key()); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 777 | populateInterfaceFromJson( |
| 778 | systemConfiguration, jsonPointerPath + boardField.key(), |
| 779 | iface.get(), boardField.value(), objServer); |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 780 | } |
| 781 | } |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 782 | |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 783 | auto exposes = boardValues.find("exposes"); |
| 784 | if (exposes == boardValues.end()) |
| 785 | { |
| 786 | continue; |
| 787 | } |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 788 | // iterate through exposes |
| 789 | jsonPointerPath += "exposes/"; |
| 790 | |
| 791 | // store the board level pointer so we can modify it on the way down |
| 792 | std::string jsonPointerPathBoard = jsonPointerPath; |
| 793 | size_t exposesIndex = -1; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 794 | for (auto &item : *exposes) |
| 795 | { |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 796 | exposesIndex++; |
| 797 | jsonPointerPath = jsonPointerPathBoard; |
| 798 | jsonPointerPath += std::to_string(exposesIndex); |
| 799 | |
James Feist | d63d18a | 2018-07-19 15:23:45 -0700 | [diff] [blame] | 800 | auto findName = item.find("Name"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 801 | if (findName == item.end()) |
| 802 | { |
| 803 | std::cerr << "cannot find name in field " << item << "\n"; |
| 804 | continue; |
| 805 | } |
| 806 | auto findStatus = item.find("status"); |
| 807 | // if status is not found it is assumed to be status = 'okay' |
| 808 | if (findStatus != item.end()) |
| 809 | { |
| 810 | if (*findStatus == "disabled") |
| 811 | { |
| 812 | continue; |
| 813 | } |
| 814 | } |
James Feist | d63d18a | 2018-07-19 15:23:45 -0700 | [diff] [blame] | 815 | auto findType = item.find("Type"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 816 | std::string itemType; |
| 817 | if (findType != item.end()) |
| 818 | { |
| 819 | itemType = findType->get<std::string>(); |
| 820 | std::regex_replace(itemType.begin(), itemType.begin(), |
| 821 | itemType.end(), ILLEGAL_DBUS_REGEX, "_"); |
| 822 | } |
| 823 | else |
| 824 | { |
| 825 | itemType = "unknown"; |
| 826 | } |
| 827 | std::string itemName = findName->get<std::string>(); |
| 828 | std::regex_replace(itemName.begin(), itemName.begin(), |
| 829 | itemName.end(), ILLEGAL_DBUS_REGEX, "_"); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 830 | auto itemIface = objServer.add_interface( |
| 831 | boardName + "/" + itemName, |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 832 | "xyz.openbmc_project.Configuration." + itemType); |
| 833 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 834 | populateInterfaceFromJson(systemConfiguration, jsonPointerPath, |
| 835 | itemIface.get(), item, objServer); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 836 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 837 | for (auto &objectPair : item.items()) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 838 | { |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 839 | jsonPointerPath = jsonPointerPathBoard + |
| 840 | std::to_string(exposesIndex) + "/" + |
| 841 | objectPair.key(); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 842 | if (objectPair.value().type() == |
| 843 | nlohmann::json::value_t::object) |
| 844 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 845 | auto objectIface = objServer.add_interface( |
| 846 | boardName + "/" + itemName, |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 847 | "xyz.openbmc_project.Configuration." + itemType + "." + |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 848 | objectPair.key()); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 849 | |
| 850 | populateInterfaceFromJson( |
| 851 | systemConfiguration, jsonPointerPath, objectIface.get(), |
| 852 | objectPair.value(), objServer); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 853 | } |
| 854 | else if (objectPair.value().type() == |
| 855 | nlohmann::json::value_t::array) |
| 856 | { |
| 857 | size_t index = 0; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 858 | if (!objectPair.value().size()) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 859 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 860 | continue; |
| 861 | } |
| 862 | bool isLegal = true; |
| 863 | auto type = objectPair.value()[0].type(); |
| 864 | if (type != nlohmann::json::value_t::object) |
| 865 | { |
| 866 | continue; |
| 867 | } |
| 868 | |
| 869 | // verify legal json |
| 870 | for (const auto &arrayItem : objectPair.value()) |
| 871 | { |
| 872 | if (arrayItem.type() != type) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 873 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 874 | isLegal = false; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 875 | break; |
| 876 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 877 | } |
| 878 | if (!isLegal) |
| 879 | { |
| 880 | std::cerr << "dbus format error" << objectPair.value() |
| 881 | << "\n"; |
| 882 | break; |
| 883 | } |
| 884 | |
| 885 | for (auto &arrayItem : objectPair.value()) |
| 886 | { |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 887 | // limit what interfaces accept set for safety |
| 888 | auto permission = |
| 889 | std::find(SETTABLE_INTERFACES.begin(), |
| 890 | SETTABLE_INTERFACES.end(), |
| 891 | objectPair.key()) != |
| 892 | SETTABLE_INTERFACES.end() |
| 893 | ? sdbusplus::asio::PropertyPermission::readWrite |
| 894 | : sdbusplus::asio::PropertyPermission::readOnly; |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 895 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 896 | auto objectIface = objServer.add_interface( |
| 897 | boardName + "/" + itemName, |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 898 | "xyz.openbmc_project.Configuration." + itemType + |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 899 | "." + objectPair.key() + std::to_string(index)); |
| 900 | populateInterfaceFromJson(systemConfiguration, |
| 901 | jsonPointerPath + "/" + |
| 902 | std::to_string(index), |
| 903 | objectIface.get(), arrayItem, |
| 904 | objServer, permission); |
| 905 | index++; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 906 | } |
| 907 | } |
| 908 | } |
| 909 | } |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | // finds the template character (currently set to $) and replaces the value with |
| 914 | // the field found in a dbus object i.e. $ADDRESS would get populated with the |
| 915 | // ADDRESS field from a object on dbus |
| 916 | void templateCharReplace( |
| 917 | nlohmann::json::iterator &keyPair, |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 918 | const boost::container::flat_map<std::string, BasicVariantType> |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 919 | &foundDevice, |
| 920 | size_t &foundDeviceIdx) |
| 921 | { |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 922 | if (keyPair.value().type() == nlohmann::json::value_t::object) |
| 923 | { |
| 924 | for (auto nextLayer = keyPair.value().begin(); |
| 925 | nextLayer != keyPair.value().end(); nextLayer++) |
| 926 | { |
| 927 | templateCharReplace(nextLayer, foundDevice, foundDeviceIdx); |
| 928 | } |
| 929 | return; |
| 930 | } |
| 931 | else if (keyPair.value().type() != nlohmann::json::value_t::string) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 932 | { |
| 933 | return; |
| 934 | } |
| 935 | |
| 936 | std::string value = keyPair.value(); |
| 937 | if (value.find(TEMPLATE_CHAR) != std::string::npos) |
| 938 | { |
| 939 | std::string templateValue = value; |
| 940 | |
| 941 | templateValue.erase(0, 1); // remove template character |
| 942 | |
| 943 | // special case index |
| 944 | if ("index" == templateValue) |
| 945 | { |
| 946 | keyPair.value() = foundDeviceIdx; |
| 947 | } |
| 948 | else |
| 949 | { |
James Feist | 13b86d6 | 2018-05-29 11:24:35 -0700 | [diff] [blame] | 950 | bool found = false; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 951 | for (auto &foundDevicePair : foundDevice) |
| 952 | { |
| 953 | if (boost::iequals(foundDevicePair.first, templateValue)) |
| 954 | { |
James Feist | 13b86d6 | 2018-05-29 11:24:35 -0700 | [diff] [blame] | 955 | mapbox::util::apply_visitor( |
| 956 | [&](auto &&val) { keyPair.value() = val; }, |
| 957 | foundDevicePair.second); |
| 958 | found = true; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 959 | break; |
| 960 | } |
| 961 | } |
James Feist | 13b86d6 | 2018-05-29 11:24:35 -0700 | [diff] [blame] | 962 | if (!found) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 963 | { |
| 964 | std::cerr << "could not find symbol " << templateValue << "\n"; |
| 965 | } |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | } |
| 969 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 970 | // reads json files out of the filesystem |
| 971 | bool findJsonFiles(std::list<nlohmann::json> &configurations) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 972 | { |
| 973 | // find configuration files |
| 974 | std::vector<fs::path> jsonPaths; |
| 975 | if (!find_files(fs::path(CONFIGURATION_DIR), R"(.*\.json)", jsonPaths, 0)) |
| 976 | { |
| 977 | std::cerr << "Unable to find any configuration files in " |
| 978 | << CONFIGURATION_DIR << "\n"; |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 979 | return false; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 980 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 981 | for (auto &jsonPath : jsonPaths) |
| 982 | { |
| 983 | std::ifstream jsonStream(jsonPath.c_str()); |
| 984 | if (!jsonStream.good()) |
| 985 | { |
| 986 | std::cerr << "unable to open " << jsonPath.string() << "\n"; |
| 987 | continue; |
| 988 | } |
| 989 | auto data = nlohmann::json::parse(jsonStream, nullptr, false); |
| 990 | if (data.is_discarded()) |
| 991 | { |
| 992 | std::cerr << "syntax error in " << jsonPath.string() << "\n"; |
| 993 | continue; |
| 994 | } |
| 995 | if (data.type() == nlohmann::json::value_t::array) |
| 996 | { |
| 997 | for (auto &d : data) |
| 998 | { |
| 999 | configurations.emplace_back(d); |
| 1000 | } |
| 1001 | } |
| 1002 | else |
| 1003 | { |
| 1004 | configurations.emplace_back(data); |
| 1005 | } |
| 1006 | } |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1007 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1008 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1009 | struct PerformScan : std::enable_shared_from_this<PerformScan> |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1010 | { |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1011 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1012 | PerformScan(nlohmann::json &systemConfiguration, |
| 1013 | std::list<nlohmann::json> &configurations, |
| 1014 | std::function<void(void)> &&callback) : |
| 1015 | _systemConfiguration(systemConfiguration), |
| 1016 | _configurations(configurations), _callback(std::move(callback)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1017 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1018 | } |
| 1019 | void run() |
| 1020 | { |
| 1021 | for (auto it = _configurations.begin(); it != _configurations.end();) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1022 | { |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1023 | auto findProbe = it->find("probe"); |
James Feist | d63d18a | 2018-07-19 15:23:45 -0700 | [diff] [blame] | 1024 | auto findName = it->find("Name"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1025 | |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1026 | nlohmann::json probeCommand; |
| 1027 | // check for poorly formatted fields, probe must be an array |
| 1028 | if (findProbe == it->end()) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1029 | { |
| 1030 | std::cerr << "configuration file missing probe:\n " << *it |
| 1031 | << "\n"; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1032 | it = _configurations.erase(it); |
| 1033 | continue; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1034 | } |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1035 | else if ((*findProbe).type() != nlohmann::json::value_t::array) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1036 | { |
| 1037 | probeCommand = nlohmann::json::array(); |
| 1038 | probeCommand.push_back(*findProbe); |
| 1039 | } |
| 1040 | else |
| 1041 | { |
| 1042 | probeCommand = *findProbe; |
| 1043 | } |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1044 | |
| 1045 | if (findName == it->end()) |
| 1046 | { |
| 1047 | std::cerr << "configuration file missing name:\n " << *it |
| 1048 | << "\n"; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1049 | it = _configurations.erase(it); |
| 1050 | continue; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1051 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1052 | std::string name = *findName; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1053 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1054 | if (std::find(PASSED_PROBES.begin(), PASSED_PROBES.end(), name) != |
| 1055 | PASSED_PROBES.end()) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1056 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1057 | it = _configurations.erase(it); |
| 1058 | continue; |
| 1059 | } |
| 1060 | nlohmann::json *record = &(*it); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1061 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1062 | // store reference to this to children to makes sure we don't get |
| 1063 | // destroyed too early |
| 1064 | auto thisRef = shared_from_this(); |
| 1065 | auto p = std::make_shared<PerformProbe>( |
| 1066 | probeCommand, |
| 1067 | [&, record, name, |
| 1068 | thisRef](std::vector<boost::container::flat_map< |
| 1069 | std::string, BasicVariantType>> &foundDevices) { |
| 1070 | _passed = true; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1071 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1072 | PASSED_PROBES.push_back(name); |
| 1073 | size_t foundDeviceIdx = 0; |
| 1074 | |
James Feist | be5425f | 2018-06-08 10:30:55 -0700 | [diff] [blame] | 1075 | // insert into configuration temporarly to be able to |
| 1076 | // reference ourselves |
| 1077 | _systemConfiguration[name] = *record; |
| 1078 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1079 | for (auto &foundDevice : foundDevices) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1080 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1081 | for (auto keyPair = record->begin(); |
| 1082 | keyPair != record->end(); keyPair++) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1083 | { |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1084 | templateCharReplace(keyPair, foundDevice, |
| 1085 | foundDeviceIdx); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1086 | } |
| 1087 | auto findExpose = record->find("exposes"); |
| 1088 | if (findExpose == record->end()) |
| 1089 | { |
| 1090 | continue; |
| 1091 | } |
| 1092 | for (auto &expose : *findExpose) |
| 1093 | { |
| 1094 | for (auto keyPair = expose.begin(); |
| 1095 | keyPair != expose.end(); keyPair++) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1096 | { |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1097 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1098 | // fill in template characters with devices |
| 1099 | // found |
| 1100 | templateCharReplace(keyPair, foundDevice, |
| 1101 | foundDeviceIdx); |
| 1102 | // special case bind |
| 1103 | if (boost::starts_with(keyPair.key(), "bind_")) |
| 1104 | { |
| 1105 | if (keyPair.value().type() != |
| 1106 | nlohmann::json::value_t::string) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1107 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1108 | std::cerr << "bind_ value must be of " |
| 1109 | "type string " |
| 1110 | << keyPair.key() << "\n"; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1111 | continue; |
| 1112 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1113 | bool foundBind = false; |
| 1114 | std::string bind = keyPair.key().substr( |
| 1115 | sizeof("bind_") - 1); |
James Feist | be5425f | 2018-06-08 10:30:55 -0700 | [diff] [blame] | 1116 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1117 | for (auto &configurationPair : |
| 1118 | _systemConfiguration.items()) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1119 | { |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1120 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1121 | auto configListFind = |
| 1122 | configurationPair.value().find( |
| 1123 | "exposes"); |
| 1124 | |
| 1125 | if (configListFind == |
| 1126 | configurationPair.value() |
| 1127 | .end() || |
| 1128 | configListFind->type() != |
| 1129 | nlohmann::json::value_t::array) |
| 1130 | { |
| 1131 | continue; |
| 1132 | } |
| 1133 | for (auto &exposedObject : |
| 1134 | *configListFind) |
| 1135 | { |
| 1136 | std::string foundObjectName = |
James Feist | d63d18a | 2018-07-19 15:23:45 -0700 | [diff] [blame] | 1137 | (exposedObject)["Name"]; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1138 | if (boost::iequals( |
| 1139 | foundObjectName, |
| 1140 | keyPair.value() |
| 1141 | .get<std::string>())) |
| 1142 | { |
| 1143 | exposedObject["status"] = |
| 1144 | "okay"; |
| 1145 | expose[bind] = exposedObject; |
| 1146 | |
| 1147 | foundBind = true; |
| 1148 | break; |
| 1149 | } |
| 1150 | } |
| 1151 | if (foundBind) |
| 1152 | { |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1153 | break; |
| 1154 | } |
| 1155 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1156 | if (!foundBind) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1157 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1158 | std::cerr << "configuration file " |
| 1159 | "dependency error, " |
| 1160 | "could not find bind " |
| 1161 | << keyPair.value() << "\n"; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1162 | } |
| 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | } |
James Feist | be5425f | 2018-06-08 10:30:55 -0700 | [diff] [blame] | 1167 | // overwrite ourselves with cleaned up version |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1168 | _systemConfiguration[name] = *record; |
| 1169 | }); |
| 1170 | p->run(); |
| 1171 | it++; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1172 | } |
| 1173 | } |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1174 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1175 | ~PerformScan() |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1176 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1177 | if (_passed) |
| 1178 | { |
| 1179 | auto nextScan = std::make_shared<PerformScan>( |
| 1180 | _systemConfiguration, _configurations, std::move(_callback)); |
| 1181 | nextScan->run(); |
| 1182 | } |
| 1183 | else |
| 1184 | { |
| 1185 | _callback(); |
| 1186 | } |
| 1187 | } |
| 1188 | nlohmann::json &_systemConfiguration; |
| 1189 | std::list<nlohmann::json> _configurations; |
| 1190 | std::function<void(void)> _callback; |
| 1191 | std::vector<std::shared_ptr<PerformProbe>> _probes; |
| 1192 | bool _passed = false; |
| 1193 | }; |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 1194 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1195 | // main properties changed entry |
| 1196 | void propertiesChangedCallback( |
| 1197 | boost::asio::io_service &io, |
| 1198 | std::vector<sdbusplus::bus::match::match> &dbusMatches, |
| 1199 | nlohmann::json &systemConfiguration, |
| 1200 | sdbusplus::asio::object_server &objServer) |
| 1201 | { |
| 1202 | static boost::asio::deadline_timer timer(io); |
| 1203 | timer.expires_from_now(boost::posix_time::seconds(1)); |
| 1204 | |
| 1205 | // setup an async wait as we normally get flooded with new requests |
| 1206 | timer.async_wait([&](const boost::system::error_code &ec) { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1207 | if (ec == boost::asio::error::operation_aborted) |
| 1208 | { |
| 1209 | // we were cancelled |
| 1210 | return; |
| 1211 | } |
| 1212 | else if (ec) |
| 1213 | { |
| 1214 | std::cerr << "async wait error " << ec << "\n"; |
| 1215 | return; |
| 1216 | } |
| 1217 | |
| 1218 | nlohmann::json oldConfiguration = systemConfiguration; |
| 1219 | DBUS_PROBE_OBJECTS.clear(); |
| 1220 | |
| 1221 | std::list<nlohmann::json> configurations; |
| 1222 | if (!findJsonFiles(configurations)) |
| 1223 | { |
| 1224 | std::cerr << "cannot find json files\n"; |
| 1225 | return; |
| 1226 | } |
| 1227 | |
| 1228 | auto perfScan = std::make_shared<PerformScan>( |
| 1229 | systemConfiguration, configurations, [&, oldConfiguration]() { |
| 1230 | nlohmann::json newConfiguration = systemConfiguration; |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1231 | for (auto it = newConfiguration.begin(); |
| 1232 | it != newConfiguration.end();) |
| 1233 | { |
| 1234 | auto findKey = oldConfiguration.find(it.key()); |
| 1235 | if (findKey != oldConfiguration.end()) |
| 1236 | { |
| 1237 | it = newConfiguration.erase(it); |
| 1238 | } |
| 1239 | else |
| 1240 | { |
| 1241 | it++; |
| 1242 | } |
| 1243 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1244 | registerCallbacks(io, dbusMatches, systemConfiguration, |
| 1245 | objServer); |
| 1246 | io.post([&, newConfiguration]() { |
| 1247 | // todo: for now, only add new configurations, |
| 1248 | // unload to come later unloadOverlays(); |
| 1249 | loadOverlays(newConfiguration); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 1250 | io.post([&]() { |
| 1251 | if (!writeJsonFiles(systemConfiguration)) |
| 1252 | { |
| 1253 | std::cerr << "Error writing json files\n"; |
| 1254 | } |
| 1255 | }); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1256 | io.post([&, newConfiguration]() { |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 1257 | postToDbus(newConfiguration, systemConfiguration, |
| 1258 | objServer); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1259 | }); |
| 1260 | }); |
| 1261 | }); |
| 1262 | perfScan->run(); |
| 1263 | }); |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1264 | } |
| 1265 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1266 | void registerCallbacks(boost::asio::io_service &io, |
| 1267 | std::vector<sdbusplus::bus::match::match> &dbusMatches, |
| 1268 | nlohmann::json &systemConfiguration, |
| 1269 | sdbusplus::asio::object_server &objServer) |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1270 | { |
| 1271 | static boost::container::flat_set<std::string> watchedObjects; |
| 1272 | |
| 1273 | for (const auto &objectMap : DBUS_PROBE_OBJECTS) |
| 1274 | { |
| 1275 | auto findObject = watchedObjects.find(objectMap.first); |
| 1276 | if (findObject != watchedObjects.end()) |
| 1277 | { |
| 1278 | continue; |
| 1279 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1280 | std::function<void(sdbusplus::message::message & message)> |
| 1281 | eventHandler = |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1282 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1283 | [&](sdbusplus::message::message &) { |
| 1284 | propertiesChangedCallback(io, dbusMatches, |
| 1285 | systemConfiguration, objServer); |
| 1286 | }; |
| 1287 | |
| 1288 | sdbusplus::bus::match::match match( |
| 1289 | static_cast<sdbusplus::bus::bus &>(*SYSTEM_BUS), |
| 1290 | "type='signal',member='PropertiesChanged',arg0='" + |
| 1291 | objectMap.first + "'", |
| 1292 | eventHandler); |
| 1293 | dbusMatches.emplace_back(std::move(match)); |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | int main(int argc, char **argv) |
| 1298 | { |
| 1299 | // setup connection to dbus |
| 1300 | boost::asio::io_service io; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1301 | SYSTEM_BUS = std::make_shared<sdbusplus::asio::connection>(io); |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1302 | SYSTEM_BUS->request_name("xyz.openbmc_project.EntityManager"); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1303 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1304 | sdbusplus::asio::object_server objServer(SYSTEM_BUS); |
James Feist | fd1264a | 2018-05-03 12:10:00 -0700 | [diff] [blame] | 1305 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1306 | std::shared_ptr<sdbusplus::asio::dbus_interface> entityIface = |
| 1307 | objServer.add_interface("/xyz/openbmc_project/EntityManager", |
| 1308 | "xyz.openbmc_project.EntityManager"); |
James Feist | fd1264a | 2018-05-03 12:10:00 -0700 | [diff] [blame] | 1309 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1310 | std::shared_ptr<sdbusplus::asio::dbus_interface> inventoryIface = |
| 1311 | objServer.add_interface("/xyz/openbmc_project/inventory", |
| 1312 | "xyz.openbmc_project.Inventory.Manager"); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1313 | |
| 1314 | // to keep reference to the match / filter objects so they don't get |
| 1315 | // destroyed |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1316 | std::vector<sdbusplus::bus::match::match> dbusMatches; |
| 1317 | |
| 1318 | nlohmann::json systemConfiguration = nlohmann::json::object(); |
| 1319 | |
| 1320 | inventoryIface->register_method( |
| 1321 | "Notify", [](const boost::container::flat_map< |
| 1322 | std::string, |
| 1323 | boost::container::flat_map<std::string, BasicVariantType>> |
| 1324 | &object) { return; }); |
| 1325 | inventoryIface->initialize(); |
| 1326 | |
| 1327 | io.post([&]() { |
| 1328 | unloadAllOverlays(); |
| 1329 | propertiesChangedCallback(io, dbusMatches, systemConfiguration, |
| 1330 | objServer); |
| 1331 | }); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1332 | |
James Feist | fd1264a | 2018-05-03 12:10:00 -0700 | [diff] [blame] | 1333 | entityIface->register_method("ReScan", [&]() { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1334 | propertiesChangedCallback(io, dbusMatches, systemConfiguration, |
| 1335 | objServer); |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1336 | }); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1337 | entityIface->initialize(); |
| 1338 | |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1339 | io.run(); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1340 | |
| 1341 | return 0; |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1342 | } |