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