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