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