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