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