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