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