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