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