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