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