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