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 | |
Igor Kononenko | 9fd87e5 | 2020-10-06 01:18:17 +0300 | [diff] [blame] | 38 | #include <charconv> |
James Feist | 637b3ef | 2019-04-15 16:35:30 -0700 | [diff] [blame] | 39 | #include <filesystem> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 40 | #include <fstream> |
| 41 | #include <iostream> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 42 | #include <regex> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 43 | #include <variant> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 44 | constexpr const char* configurationDirectory = PACKAGE_DIR "configurations"; |
| 45 | constexpr const char* schemaDirectory = PACKAGE_DIR "configurations/schemas"; |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 46 | constexpr const char* tempConfigDir = "/tmp/configuration/"; |
| 47 | constexpr const char* lastConfiguration = "/tmp/configuration/last.json"; |
| 48 | constexpr const char* currentConfiguration = "/var/configuration/system.json"; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 49 | constexpr const char* globalSchema = "global.json"; |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 50 | constexpr const bool debug = false; |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 51 | |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 52 | static const boost::container::flat_map<const char*, probe_type_codes, CmpStr> |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 53 | probeTypes{{{"FALSE", probe_type_codes::FALSE_T}, |
| 54 | {"TRUE", probe_type_codes::TRUE_T}, |
| 55 | {"AND", probe_type_codes::AND}, |
| 56 | {"OR", probe_type_codes::OR}, |
| 57 | {"FOUND", probe_type_codes::FOUND}, |
| 58 | {"MATCH_ONE", probe_type_codes::MATCH_ONE}}}; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 59 | |
Adrian Ambrożewicz | c789fca | 2020-05-14 15:50:05 +0200 | [diff] [blame] | 60 | static constexpr std::array<const char*, 6> settableInterfaces = { |
| 61 | "FanProfile", "Pid", "Pid.Zone", "Stepwise", "Thresholds", "Polling"}; |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 62 | using JsonVariantType = |
James Feist | 338b8a7 | 2019-03-01 10:16:45 -0800 | [diff] [blame] | 63 | std::variant<std::vector<std::string>, std::vector<double>, std::string, |
| 64 | int64_t, uint64_t, double, int32_t, uint32_t, int16_t, |
| 65 | uint16_t, uint8_t, bool>; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 66 | |
| 67 | using ManagedObjectType = boost::container::flat_map< |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 68 | sdbusplus::message::object_path, |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 69 | boost::container::flat_map< |
| 70 | std::string, |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 71 | boost::container::flat_map<std::string, BasicVariantType>>>; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 72 | |
James Feist | d58879a8 | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 73 | // store reference to all interfaces so we can destroy them later |
| 74 | boost::container::flat_map< |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 75 | std::string, std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>> |
James Feist | d58879a8 | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 76 | inventory; |
| 77 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 78 | // todo: pass this through nicer |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 79 | std::shared_ptr<sdbusplus::asio::connection> systemBus; |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 80 | nlohmann::json lastJson; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 81 | |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 82 | boost::asio::io_context io; |
| 83 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 84 | const std::regex illegalDbusPathRegex("[^A-Za-z0-9_.]"); |
| 85 | const std::regex illegalDbusMemberRegex("[^A-Za-z0-9_]"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 86 | |
James Feist | d58879a8 | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 87 | static std::shared_ptr<sdbusplus::asio::dbus_interface> |
| 88 | createInterface(sdbusplus::asio::object_server& objServer, |
| 89 | const std::string& path, const std::string& interface, |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 90 | const std::string& parent, bool checkNull = false) |
James Feist | d58879a8 | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 91 | { |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 92 | // on first add we have no reason to check for null before add, as there |
| 93 | // won't be any. For dynamically added interfaces, we check for null so that |
| 94 | // a constant delete/add will not create a memory leak |
| 95 | |
| 96 | auto ptr = objServer.add_interface(path, interface); |
| 97 | auto& dataVector = inventory[parent]; |
| 98 | if (checkNull) |
| 99 | { |
| 100 | auto it = std::find_if(dataVector.begin(), dataVector.end(), |
| 101 | [](const auto& p) { return p.expired(); }); |
| 102 | if (it != dataVector.end()) |
| 103 | { |
| 104 | *it = ptr; |
| 105 | return ptr; |
| 106 | } |
| 107 | } |
| 108 | dataVector.emplace_back(ptr); |
| 109 | return ptr; |
James Feist | d58879a8 | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 110 | } |
| 111 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 112 | // writes output files to persist data |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 113 | bool writeJsonFiles(const nlohmann::json& systemConfiguration) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 114 | { |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 115 | std::filesystem::create_directory(configurationOutDir); |
| 116 | std::ofstream output(currentConfiguration); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 117 | if (!output.good()) |
| 118 | { |
| 119 | return false; |
| 120 | } |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 121 | output << systemConfiguration.dump(4); |
| 122 | output.close(); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 123 | return true; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 124 | } |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 125 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 126 | template <typename JsonType> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 127 | bool setJsonFromPointer(const std::string& ptrStr, const JsonType& value, |
| 128 | nlohmann::json& systemConfiguration) |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 129 | { |
| 130 | try |
| 131 | { |
| 132 | nlohmann::json::json_pointer ptr(ptrStr); |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 133 | nlohmann::json& ref = systemConfiguration[ptr]; |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 134 | ref = value; |
| 135 | return true; |
| 136 | } |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 137 | catch (const std::out_of_range&) |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 138 | { |
| 139 | return false; |
| 140 | } |
| 141 | } |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 142 | |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 143 | // template function to add array as dbus property |
| 144 | template <typename PropertyType> |
| 145 | void addArrayToDbus(const std::string& name, const nlohmann::json& array, |
| 146 | sdbusplus::asio::dbus_interface* iface, |
| 147 | sdbusplus::asio::PropertyPermission permission, |
| 148 | nlohmann::json& systemConfiguration, |
| 149 | const std::string& jsonPointerString) |
| 150 | { |
| 151 | std::vector<PropertyType> values; |
| 152 | for (const auto& property : array) |
| 153 | { |
| 154 | auto ptr = property.get_ptr<const PropertyType*>(); |
| 155 | if (ptr != nullptr) |
| 156 | { |
| 157 | values.emplace_back(*ptr); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if (permission == sdbusplus::asio::PropertyPermission::readOnly) |
| 162 | { |
| 163 | iface->register_property(name, values); |
| 164 | } |
| 165 | else |
| 166 | { |
| 167 | iface->register_property( |
| 168 | name, values, |
| 169 | [&systemConfiguration, |
| 170 | jsonPointerString{std::string(jsonPointerString)}]( |
| 171 | const std::vector<PropertyType>& newVal, |
| 172 | std::vector<PropertyType>& val) { |
| 173 | val = newVal; |
| 174 | if (!setJsonFromPointer(jsonPointerString, val, |
| 175 | systemConfiguration)) |
| 176 | { |
| 177 | std::cerr << "error setting json field\n"; |
| 178 | return -1; |
| 179 | } |
| 180 | if (!writeJsonFiles(systemConfiguration)) |
| 181 | { |
| 182 | std::cerr << "error setting json file\n"; |
| 183 | return -1; |
| 184 | } |
| 185 | return 1; |
| 186 | }); |
| 187 | } |
| 188 | } |
| 189 | |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 190 | template <typename PropertyType> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 191 | void addProperty(const std::string& propertyName, const PropertyType& value, |
| 192 | sdbusplus::asio::dbus_interface* iface, |
| 193 | nlohmann::json& systemConfiguration, |
| 194 | const std::string& jsonPointerString, |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 195 | sdbusplus::asio::PropertyPermission permission) |
| 196 | { |
| 197 | if (permission == sdbusplus::asio::PropertyPermission::readOnly) |
| 198 | { |
| 199 | iface->register_property(propertyName, value); |
| 200 | return; |
| 201 | } |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 202 | iface->register_property( |
| 203 | propertyName, value, |
| 204 | [&systemConfiguration, |
| 205 | jsonPointerString{std::string(jsonPointerString)}]( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 206 | const PropertyType& newVal, PropertyType& val) { |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 207 | val = newVal; |
| 208 | if (!setJsonFromPointer(jsonPointerString, val, |
| 209 | systemConfiguration)) |
| 210 | { |
| 211 | std::cerr << "error setting json field\n"; |
| 212 | return -1; |
| 213 | } |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 214 | if (!writeJsonFiles(systemConfiguration)) |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 215 | { |
| 216 | std::cerr << "error setting json file\n"; |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 217 | return -1; |
| 218 | } |
| 219 | return 1; |
| 220 | }); |
| 221 | } |
| 222 | |
| 223 | void createDeleteObjectMethod( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 224 | const std::string& jsonPointerPath, |
| 225 | const std::shared_ptr<sdbusplus::asio::dbus_interface>& iface, |
| 226 | sdbusplus::asio::object_server& objServer, |
| 227 | nlohmann::json& systemConfiguration) |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 228 | { |
| 229 | std::weak_ptr<sdbusplus::asio::dbus_interface> interface = iface; |
| 230 | iface->register_method( |
| 231 | "Delete", [&objServer, &systemConfiguration, interface, |
| 232 | jsonPointerPath{std::string(jsonPointerPath)}]() { |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 233 | std::shared_ptr<sdbusplus::asio::dbus_interface> dbusInterface = |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 234 | interface.lock(); |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 235 | if (!dbusInterface) |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 236 | { |
| 237 | // this technically can't happen as the pointer is pointing to |
| 238 | // us |
| 239 | throw DBusInternalError(); |
| 240 | } |
| 241 | nlohmann::json::json_pointer ptr(jsonPointerPath); |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 242 | systemConfiguration[ptr] = nullptr; |
| 243 | |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 244 | // todo(james): dig through sdbusplus to find out why we can't |
| 245 | // delete it in a method call |
| 246 | io.post([&objServer, dbusInterface]() mutable { |
| 247 | objServer.remove_interface(dbusInterface); |
| 248 | }); |
| 249 | |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 250 | if (!writeJsonFiles(systemConfiguration)) |
| 251 | { |
| 252 | std::cerr << "error setting json file\n"; |
| 253 | throw DBusInternalError(); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 254 | } |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 255 | }); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 256 | } |
| 257 | |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 258 | // adds simple json types to interface's properties |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 259 | void populateInterfaceFromJson( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 260 | nlohmann::json& systemConfiguration, const std::string& jsonPointerPath, |
| 261 | std::shared_ptr<sdbusplus::asio::dbus_interface>& iface, |
| 262 | nlohmann::json& dict, sdbusplus::asio::object_server& objServer, |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 263 | sdbusplus::asio::PropertyPermission permission = |
| 264 | sdbusplus::asio::PropertyPermission::readOnly) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 265 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 266 | for (auto& dictPair : dict.items()) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 267 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 268 | auto type = dictPair.value().type(); |
| 269 | bool array = false; |
| 270 | if (dictPair.value().type() == nlohmann::json::value_t::array) |
| 271 | { |
| 272 | array = true; |
| 273 | if (!dictPair.value().size()) |
| 274 | { |
| 275 | continue; |
| 276 | } |
| 277 | type = dictPair.value()[0].type(); |
| 278 | bool isLegal = true; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 279 | for (const auto& arrayItem : dictPair.value()) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 280 | { |
| 281 | if (arrayItem.type() != type) |
| 282 | { |
| 283 | isLegal = false; |
| 284 | break; |
| 285 | } |
| 286 | } |
| 287 | if (!isLegal) |
| 288 | { |
| 289 | std::cerr << "dbus format error" << dictPair.value() << "\n"; |
| 290 | continue; |
| 291 | } |
James Feist | a218ddb | 2019-04-11 14:01:31 -0700 | [diff] [blame] | 292 | } |
| 293 | if (type == nlohmann::json::value_t::object) |
| 294 | { |
| 295 | continue; // handled elsewhere |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 296 | } |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 297 | std::string key = jsonPointerPath + "/" + dictPair.key(); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 298 | if (permission == sdbusplus::asio::PropertyPermission::readWrite) |
| 299 | { |
| 300 | // all setable numbers are doubles as it is difficult to always |
| 301 | // create a configuration file with all whole numbers as decimals |
| 302 | // i.e. 1.0 |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 303 | if (array) |
| 304 | { |
| 305 | if (dictPair.value()[0].is_number()) |
| 306 | { |
| 307 | type = nlohmann::json::value_t::number_float; |
| 308 | } |
| 309 | } |
| 310 | else if (dictPair.value().is_number()) |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 311 | { |
| 312 | type = nlohmann::json::value_t::number_float; |
| 313 | } |
| 314 | } |
| 315 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 316 | switch (type) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 317 | { |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 318 | case (nlohmann::json::value_t::boolean): |
| 319 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 320 | if (array) |
| 321 | { |
| 322 | // todo: array of bool isn't detected correctly by |
| 323 | // sdbusplus, change it to numbers |
| 324 | addArrayToDbus<uint64_t>(dictPair.key(), dictPair.value(), |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 325 | iface.get(), permission, |
| 326 | systemConfiguration, key); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 327 | } |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 328 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 329 | else |
| 330 | { |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 331 | addProperty(dictPair.key(), dictPair.value().get<bool>(), |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 332 | iface.get(), systemConfiguration, key, |
| 333 | permission); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 334 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 335 | break; |
| 336 | } |
| 337 | case (nlohmann::json::value_t::number_integer): |
| 338 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 339 | if (array) |
| 340 | { |
| 341 | addArrayToDbus<int64_t>(dictPair.key(), dictPair.value(), |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 342 | iface.get(), permission, |
| 343 | systemConfiguration, key); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 344 | } |
| 345 | else |
| 346 | { |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 347 | addProperty(dictPair.key(), dictPair.value().get<int64_t>(), |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 348 | iface.get(), systemConfiguration, key, |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 349 | sdbusplus::asio::PropertyPermission::readOnly); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 350 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 351 | break; |
| 352 | } |
| 353 | case (nlohmann::json::value_t::number_unsigned): |
| 354 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 355 | if (array) |
| 356 | { |
| 357 | addArrayToDbus<uint64_t>(dictPair.key(), dictPair.value(), |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 358 | iface.get(), permission, |
| 359 | systemConfiguration, key); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 360 | } |
| 361 | else |
| 362 | { |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 363 | addProperty(dictPair.key(), |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 364 | dictPair.value().get<uint64_t>(), iface.get(), |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 365 | systemConfiguration, key, |
| 366 | sdbusplus::asio::PropertyPermission::readOnly); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 367 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 368 | break; |
| 369 | } |
| 370 | case (nlohmann::json::value_t::number_float): |
| 371 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 372 | if (array) |
| 373 | { |
| 374 | addArrayToDbus<double>(dictPair.key(), dictPair.value(), |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 375 | iface.get(), permission, |
| 376 | systemConfiguration, key); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 377 | } |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 378 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 379 | else |
| 380 | { |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 381 | addProperty(dictPair.key(), dictPair.value().get<double>(), |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 382 | iface.get(), systemConfiguration, key, |
| 383 | permission); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 384 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 385 | break; |
| 386 | } |
| 387 | case (nlohmann::json::value_t::string): |
| 388 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 389 | if (array) |
| 390 | { |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 391 | addArrayToDbus<std::string>( |
| 392 | dictPair.key(), dictPair.value(), iface.get(), |
| 393 | permission, systemConfiguration, key); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 394 | } |
| 395 | else |
| 396 | { |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 397 | addProperty( |
| 398 | dictPair.key(), dictPair.value().get<std::string>(), |
| 399 | iface.get(), systemConfiguration, key, permission); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 400 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 401 | break; |
| 402 | } |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 403 | default: |
| 404 | { |
James Feist | a218ddb | 2019-04-11 14:01:31 -0700 | [diff] [blame] | 405 | std::cerr << "Unexpected json type in system configuration " |
| 406 | << dictPair.key() << ": " |
| 407 | << dictPair.value().type_name() << "\n"; |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 408 | break; |
| 409 | } |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 410 | } |
| 411 | } |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 412 | if (permission == sdbusplus::asio::PropertyPermission::readWrite) |
| 413 | { |
| 414 | createDeleteObjectMethod(jsonPointerPath, iface, objServer, |
| 415 | systemConfiguration); |
| 416 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 417 | iface->initialize(); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 418 | } |
| 419 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 420 | sdbusplus::asio::PropertyPermission getPermission(const std::string& interface) |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 421 | { |
| 422 | return std::find(settableInterfaces.begin(), settableInterfaces.end(), |
| 423 | interface) != settableInterfaces.end() |
| 424 | ? sdbusplus::asio::PropertyPermission::readWrite |
| 425 | : sdbusplus::asio::PropertyPermission::readOnly; |
| 426 | } |
| 427 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 428 | void createAddObjectMethod(const std::string& jsonPointerPath, |
| 429 | const std::string& path, |
| 430 | nlohmann::json& systemConfiguration, |
James Feist | d58879a8 | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 431 | sdbusplus::asio::object_server& objServer, |
| 432 | const std::string& board) |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 433 | { |
James Feist | d58879a8 | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 434 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = createInterface( |
| 435 | objServer, path, "xyz.openbmc_project.AddObject", board); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 436 | |
| 437 | iface->register_method( |
| 438 | "AddObject", |
| 439 | [&systemConfiguration, &objServer, |
James Feist | d58879a8 | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 440 | jsonPointerPath{std::string(jsonPointerPath)}, path{std::string(path)}, |
| 441 | board](const boost::container::flat_map<std::string, JsonVariantType>& |
| 442 | data) { |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 443 | nlohmann::json::json_pointer ptr(jsonPointerPath); |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 444 | nlohmann::json& base = systemConfiguration[ptr]; |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 445 | auto findExposes = base.find("Exposes"); |
| 446 | |
| 447 | if (findExposes == base.end()) |
| 448 | { |
| 449 | throw std::invalid_argument("Entity must have children."); |
| 450 | } |
| 451 | |
| 452 | // this will throw invalid-argument to sdbusplus if invalid json |
| 453 | nlohmann::json newData{}; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 454 | for (const auto& item : data) |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 455 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 456 | nlohmann::json& newJson = newData[item.first]; |
| 457 | std::visit([&newJson](auto&& val) { newJson = std::move(val); }, |
| 458 | item.second); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | auto findName = newData.find("Name"); |
| 462 | auto findType = newData.find("Type"); |
| 463 | if (findName == newData.end() || findType == newData.end()) |
| 464 | { |
| 465 | throw std::invalid_argument("AddObject missing Name or Type"); |
| 466 | } |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 467 | const std::string* type = findType->get_ptr<const std::string*>(); |
| 468 | const std::string* name = findName->get_ptr<const std::string*>(); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 469 | if (type == nullptr || name == nullptr) |
| 470 | { |
| 471 | throw std::invalid_argument("Type and Name must be a string."); |
| 472 | } |
| 473 | |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 474 | bool foundNull = false; |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 475 | size_t lastIndex = 0; |
| 476 | // we add in the "exposes" |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 477 | for (const auto& expose : *findExposes) |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 478 | { |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 479 | if (expose.is_null()) |
| 480 | { |
| 481 | foundNull = true; |
| 482 | continue; |
| 483 | } |
| 484 | |
| 485 | if (expose["Name"] == *name && expose["Type"] == *type) |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 486 | { |
| 487 | throw std::invalid_argument( |
| 488 | "Field already in JSON, not adding"); |
| 489 | } |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 490 | |
| 491 | if (foundNull) |
| 492 | { |
| 493 | continue; |
| 494 | } |
| 495 | |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 496 | lastIndex++; |
| 497 | } |
| 498 | |
| 499 | std::ifstream schemaFile(std::string(schemaDirectory) + "/" + |
| 500 | *type + ".json"); |
| 501 | // todo(james) we might want to also make a list of 'can add' |
| 502 | // interfaces but for now I think the assumption if there is a |
| 503 | // schema avaliable that it is allowed to update is fine |
| 504 | if (!schemaFile.good()) |
| 505 | { |
| 506 | throw std::invalid_argument( |
| 507 | "No schema avaliable, cannot validate."); |
| 508 | } |
| 509 | nlohmann::json schema = |
| 510 | nlohmann::json::parse(schemaFile, nullptr, false); |
| 511 | if (schema.is_discarded()) |
| 512 | { |
| 513 | std::cerr << "Schema not legal" << *type << ".json\n"; |
| 514 | throw DBusInternalError(); |
| 515 | } |
| 516 | if (!validateJson(schema, newData)) |
| 517 | { |
| 518 | throw std::invalid_argument("Data does not match schema"); |
| 519 | } |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 520 | if (foundNull) |
| 521 | { |
| 522 | findExposes->at(lastIndex) = newData; |
| 523 | } |
| 524 | else |
| 525 | { |
| 526 | findExposes->push_back(newData); |
| 527 | } |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 528 | if (!writeJsonFiles(systemConfiguration)) |
| 529 | { |
| 530 | std::cerr << "Error writing json files\n"; |
| 531 | throw DBusInternalError(); |
| 532 | } |
| 533 | std::string dbusName = *name; |
| 534 | |
| 535 | std::regex_replace(dbusName.begin(), dbusName.begin(), |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 536 | dbusName.end(), illegalDbusMemberRegex, "_"); |
James Feist | d58879a8 | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 537 | |
| 538 | std::shared_ptr<sdbusplus::asio::dbus_interface> interface = |
| 539 | createInterface(objServer, path + "/" + dbusName, |
| 540 | "xyz.openbmc_project.Configuration." + *type, |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 541 | board, true); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 542 | // permission is read-write, as since we just created it, must be |
| 543 | // runtime modifiable |
| 544 | populateInterfaceFromJson( |
| 545 | systemConfiguration, |
James Feist | 16a02f2 | 2019-05-13 15:21:37 -0700 | [diff] [blame] | 546 | jsonPointerPath + "/Exposes/" + std::to_string(lastIndex), |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 547 | interface, newData, objServer, |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 548 | sdbusplus::asio::PropertyPermission::readWrite); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 549 | }); |
| 550 | iface->initialize(); |
| 551 | } |
| 552 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 553 | void postToDbus(const nlohmann::json& newConfiguration, |
| 554 | nlohmann::json& systemConfiguration, |
| 555 | sdbusplus::asio::object_server& objServer) |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 556 | |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 557 | { |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 558 | // iterate through boards |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 559 | for (auto& boardPair : newConfiguration.items()) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 560 | { |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 561 | std::string boardKey = boardPair.value()["Name"]; |
James Feist | d58879a8 | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 562 | std::string boardKeyOrig = boardPair.value()["Name"]; |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 563 | std::string jsonPointerPath = "/" + boardPair.key(); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 564 | // loop through newConfiguration, but use values from system |
| 565 | // configuration to be able to modify via dbus later |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 566 | auto boardValues = systemConfiguration[boardPair.key()]; |
James Feist | d63d18a | 2018-07-19 15:23:45 -0700 | [diff] [blame] | 567 | auto findBoardType = boardValues.find("Type"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 568 | std::string boardType; |
| 569 | if (findBoardType != boardValues.end() && |
| 570 | findBoardType->type() == nlohmann::json::value_t::string) |
| 571 | { |
| 572 | boardType = findBoardType->get<std::string>(); |
| 573 | std::regex_replace(boardType.begin(), boardType.begin(), |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 574 | boardType.end(), illegalDbusMemberRegex, "_"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 575 | } |
| 576 | else |
| 577 | { |
| 578 | std::cerr << "Unable to find type for " << boardKey |
| 579 | << " reverting to Chassis.\n"; |
| 580 | boardType = "Chassis"; |
| 581 | } |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 582 | std::string boardtypeLower = boost::algorithm::to_lower_copy(boardType); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 583 | |
| 584 | std::regex_replace(boardKey.begin(), boardKey.begin(), boardKey.end(), |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 585 | illegalDbusMemberRegex, "_"); |
| 586 | std::string boardName = "/xyz/openbmc_project/inventory/system/"; |
| 587 | boardName += boardtypeLower; |
| 588 | boardName += "/"; |
| 589 | boardName += boardKey; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 590 | |
James Feist | d58879a8 | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 591 | std::shared_ptr<sdbusplus::asio::dbus_interface> inventoryIface = |
| 592 | createInterface(objServer, boardName, |
| 593 | "xyz.openbmc_project.Inventory.Item", boardKey); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 594 | |
James Feist | d58879a8 | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 595 | std::shared_ptr<sdbusplus::asio::dbus_interface> boardIface = |
| 596 | createInterface(objServer, boardName, |
| 597 | "xyz.openbmc_project.Inventory.Item." + boardType, |
| 598 | boardKeyOrig); |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 599 | |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 600 | createAddObjectMethod(jsonPointerPath, boardName, systemConfiguration, |
James Feist | d58879a8 | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 601 | objServer, boardKeyOrig); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 602 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 603 | populateInterfaceFromJson(systemConfiguration, jsonPointerPath, |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 604 | boardIface, boardValues, objServer); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 605 | jsonPointerPath += "/"; |
| 606 | // iterate through board properties |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 607 | for (auto& boardField : boardValues.items()) |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 608 | { |
| 609 | if (boardField.value().type() == nlohmann::json::value_t::object) |
| 610 | { |
James Feist | d58879a8 | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 611 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
| 612 | createInterface(objServer, boardName, boardField.key(), |
| 613 | boardKeyOrig); |
| 614 | |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 615 | populateInterfaceFromJson(systemConfiguration, |
| 616 | jsonPointerPath + boardField.key(), |
| 617 | iface, boardField.value(), objServer); |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 618 | } |
| 619 | } |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 620 | |
James Feist | 1e3e698 | 2018-08-03 16:09:28 -0700 | [diff] [blame] | 621 | auto exposes = boardValues.find("Exposes"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 622 | if (exposes == boardValues.end()) |
| 623 | { |
| 624 | continue; |
| 625 | } |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 626 | // iterate through exposes |
James Feist | 1e3e698 | 2018-08-03 16:09:28 -0700 | [diff] [blame] | 627 | jsonPointerPath += "Exposes/"; |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 628 | |
| 629 | // store the board level pointer so we can modify it on the way down |
| 630 | std::string jsonPointerPathBoard = jsonPointerPath; |
| 631 | size_t exposesIndex = -1; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 632 | for (auto& item : *exposes) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 633 | { |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 634 | exposesIndex++; |
| 635 | jsonPointerPath = jsonPointerPathBoard; |
| 636 | jsonPointerPath += std::to_string(exposesIndex); |
| 637 | |
James Feist | d63d18a | 2018-07-19 15:23:45 -0700 | [diff] [blame] | 638 | auto findName = item.find("Name"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 639 | if (findName == item.end()) |
| 640 | { |
| 641 | std::cerr << "cannot find name in field " << item << "\n"; |
| 642 | continue; |
| 643 | } |
James Feist | 1e3e698 | 2018-08-03 16:09:28 -0700 | [diff] [blame] | 644 | auto findStatus = item.find("Status"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 645 | // if status is not found it is assumed to be status = 'okay' |
| 646 | if (findStatus != item.end()) |
| 647 | { |
| 648 | if (*findStatus == "disabled") |
| 649 | { |
| 650 | continue; |
| 651 | } |
| 652 | } |
James Feist | d63d18a | 2018-07-19 15:23:45 -0700 | [diff] [blame] | 653 | auto findType = item.find("Type"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 654 | std::string itemType; |
| 655 | if (findType != item.end()) |
| 656 | { |
| 657 | itemType = findType->get<std::string>(); |
| 658 | std::regex_replace(itemType.begin(), itemType.begin(), |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 659 | itemType.end(), illegalDbusPathRegex, "_"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 660 | } |
| 661 | else |
| 662 | { |
| 663 | itemType = "unknown"; |
| 664 | } |
| 665 | std::string itemName = findName->get<std::string>(); |
| 666 | std::regex_replace(itemName.begin(), itemName.begin(), |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 667 | itemName.end(), illegalDbusMemberRegex, "_"); |
| 668 | std::string ifacePath = boardName; |
| 669 | ifacePath += "/"; |
| 670 | ifacePath += itemName; |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 671 | |
James Feist | d58879a8 | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 672 | std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface = |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 673 | createInterface(objServer, ifacePath, |
James Feist | d58879a8 | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 674 | "xyz.openbmc_project.Configuration." + itemType, |
| 675 | boardKeyOrig); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 676 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 677 | populateInterfaceFromJson(systemConfiguration, jsonPointerPath, |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 678 | itemIface, item, objServer, |
| 679 | getPermission(itemType)); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 680 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 681 | for (auto& objectPair : item.items()) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 682 | { |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 683 | jsonPointerPath = jsonPointerPathBoard + |
| 684 | std::to_string(exposesIndex) + "/" + |
| 685 | objectPair.key(); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 686 | if (objectPair.value().type() == |
| 687 | nlohmann::json::value_t::object) |
| 688 | { |
James Feist | d58879a8 | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 689 | std::shared_ptr<sdbusplus::asio::dbus_interface> |
| 690 | objectIface = createInterface( |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 691 | objServer, ifacePath, |
James Feist | d58879a8 | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 692 | "xyz.openbmc_project.Configuration." + itemType + |
| 693 | "." + objectPair.key(), |
| 694 | boardKeyOrig); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 695 | |
Adrian Ambrożewicz | c789fca | 2020-05-14 15:50:05 +0200 | [diff] [blame] | 696 | populateInterfaceFromJson(systemConfiguration, |
| 697 | jsonPointerPath, objectIface, |
| 698 | objectPair.value(), objServer, |
| 699 | getPermission(objectPair.key())); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 700 | } |
| 701 | else if (objectPair.value().type() == |
| 702 | nlohmann::json::value_t::array) |
| 703 | { |
| 704 | size_t index = 0; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 705 | if (!objectPair.value().size()) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 706 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 707 | continue; |
| 708 | } |
| 709 | bool isLegal = true; |
| 710 | auto type = objectPair.value()[0].type(); |
| 711 | if (type != nlohmann::json::value_t::object) |
| 712 | { |
| 713 | continue; |
| 714 | } |
| 715 | |
| 716 | // verify legal json |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 717 | for (const auto& arrayItem : objectPair.value()) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 718 | { |
| 719 | if (arrayItem.type() != type) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 720 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 721 | isLegal = false; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 722 | break; |
| 723 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 724 | } |
| 725 | if (!isLegal) |
| 726 | { |
| 727 | std::cerr << "dbus format error" << objectPair.value() |
| 728 | << "\n"; |
| 729 | break; |
| 730 | } |
| 731 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 732 | for (auto& arrayItem : objectPair.value()) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 733 | { |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 734 | |
James Feist | d58879a8 | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 735 | std::shared_ptr<sdbusplus::asio::dbus_interface> |
| 736 | objectIface = createInterface( |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 737 | objServer, ifacePath, |
James Feist | d58879a8 | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 738 | "xyz.openbmc_project.Configuration." + |
| 739 | itemType + "." + objectPair.key() + |
| 740 | std::to_string(index), |
| 741 | boardKeyOrig); |
| 742 | |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 743 | populateInterfaceFromJson( |
| 744 | systemConfiguration, |
| 745 | jsonPointerPath + "/" + std::to_string(index), |
| 746 | objectIface, arrayItem, objServer, |
| 747 | getPermission(objectPair.key())); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 748 | index++; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 756 | // reads json files out of the filesystem |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 757 | bool findJsonFiles(std::list<nlohmann::json>& configurations) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 758 | { |
| 759 | // find configuration files |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 760 | std::vector<std::filesystem::path> jsonPaths; |
Johnathan Mantey | 2015f75 | 2019-03-26 15:22:31 -0700 | [diff] [blame] | 761 | if (!findFiles(std::filesystem::path(configurationDirectory), R"(.*\.json)", |
| 762 | jsonPaths)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 763 | { |
| 764 | std::cerr << "Unable to find any configuration files in " |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 765 | << configurationDirectory << "\n"; |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 766 | return false; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 767 | } |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 768 | |
| 769 | std::ifstream schemaStream(std::string(schemaDirectory) + "/" + |
| 770 | globalSchema); |
| 771 | if (!schemaStream.good()) |
| 772 | { |
| 773 | std::cerr |
| 774 | << "Cannot open schema file, cannot validate JSON, exiting\n\n"; |
| 775 | std::exit(EXIT_FAILURE); |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 776 | return false; |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 777 | } |
| 778 | nlohmann::json schema = nlohmann::json::parse(schemaStream, nullptr, false); |
| 779 | if (schema.is_discarded()) |
| 780 | { |
| 781 | std::cerr |
| 782 | << "Illegal schema file detected, cannot validate JSON, exiting\n"; |
| 783 | std::exit(EXIT_FAILURE); |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 784 | return false; |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 785 | } |
| 786 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 787 | for (auto& jsonPath : jsonPaths) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 788 | { |
| 789 | std::ifstream jsonStream(jsonPath.c_str()); |
| 790 | if (!jsonStream.good()) |
| 791 | { |
| 792 | std::cerr << "unable to open " << jsonPath.string() << "\n"; |
| 793 | continue; |
| 794 | } |
| 795 | auto data = nlohmann::json::parse(jsonStream, nullptr, false); |
| 796 | if (data.is_discarded()) |
| 797 | { |
| 798 | std::cerr << "syntax error in " << jsonPath.string() << "\n"; |
| 799 | continue; |
| 800 | } |
James Feist | 8da9919 | 2019-01-24 08:20:16 -0800 | [diff] [blame] | 801 | /* |
| 802 | * todo(james): reenable this once less things are in flight |
| 803 | * |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 804 | if (!validateJson(schema, data)) |
| 805 | { |
| 806 | std::cerr << "Error validating " << jsonPath.string() << "\n"; |
| 807 | continue; |
| 808 | } |
James Feist | 8da9919 | 2019-01-24 08:20:16 -0800 | [diff] [blame] | 809 | */ |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 810 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 811 | if (data.type() == nlohmann::json::value_t::array) |
| 812 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 813 | for (auto& d : data) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 814 | { |
| 815 | configurations.emplace_back(d); |
| 816 | } |
| 817 | } |
| 818 | else |
| 819 | { |
| 820 | configurations.emplace_back(data); |
| 821 | } |
| 822 | } |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 823 | return true; |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 824 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 825 | |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 826 | void startRemovedTimer(boost::asio::steady_timer& timer, |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 827 | nlohmann::json& systemConfiguration) |
| 828 | { |
| 829 | static bool scannedPowerOff = false; |
| 830 | static bool scannedPowerOn = false; |
| 831 | |
James Feist | fb00f39 | 2019-06-25 14:16:48 -0700 | [diff] [blame] | 832 | if (systemConfiguration.empty() || lastJson.empty()) |
| 833 | { |
| 834 | return; // not ready yet |
| 835 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 836 | if (scannedPowerOn) |
| 837 | { |
| 838 | return; |
| 839 | } |
| 840 | |
| 841 | if (!isPowerOn() && scannedPowerOff) |
| 842 | { |
| 843 | return; |
| 844 | } |
| 845 | |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 846 | timer.expires_after(std::chrono::seconds(10)); |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 847 | timer.async_wait( |
| 848 | [&systemConfiguration](const boost::system::error_code& ec) { |
| 849 | if (ec == boost::asio::error::operation_aborted) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 850 | { |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 851 | // we were cancelled |
| 852 | return; |
| 853 | } |
| 854 | |
| 855 | bool powerOff = !isPowerOn(); |
| 856 | for (const auto& item : lastJson.items()) |
| 857 | { |
| 858 | if (systemConfiguration.find(item.key()) == |
| 859 | systemConfiguration.end()) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 860 | { |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 861 | bool isDetectedPowerOn = false; |
| 862 | auto powerState = item.value().find("PowerState"); |
| 863 | if (powerState != item.value().end()) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 864 | { |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 865 | auto ptr = powerState->get_ptr<const std::string*>(); |
| 866 | if (ptr) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 867 | { |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 868 | if (*ptr == "On" || *ptr == "BiosPost") |
| 869 | { |
| 870 | isDetectedPowerOn = true; |
| 871 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 872 | } |
| 873 | } |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 874 | if (powerOff && isDetectedPowerOn) |
| 875 | { |
| 876 | // power not on yet, don't know if it's there or not |
| 877 | continue; |
| 878 | } |
| 879 | if (!powerOff && scannedPowerOff && isDetectedPowerOn) |
| 880 | { |
| 881 | // already logged it when power was off |
| 882 | continue; |
| 883 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 884 | |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 885 | logDeviceRemoved(item.value()); |
| 886 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 887 | } |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 888 | scannedPowerOff = true; |
| 889 | if (!powerOff) |
| 890 | { |
| 891 | scannedPowerOn = true; |
| 892 | } |
| 893 | }); |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 894 | } |
| 895 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 896 | // main properties changed entry |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 897 | void propertiesChangedCallback(nlohmann::json& systemConfiguration, |
| 898 | sdbusplus::asio::object_server& objServer) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 899 | { |
James Feist | 2539ccd | 2020-05-01 16:15:08 -0700 | [diff] [blame] | 900 | static bool inProgress = false; |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 901 | static boost::asio::steady_timer timer(io); |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 902 | static size_t instance = 0; |
| 903 | instance++; |
| 904 | size_t count = instance; |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 905 | |
James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 906 | timer.expires_after(std::chrono::seconds(5)); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 907 | |
| 908 | // setup an async wait as we normally get flooded with new requests |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 909 | timer.async_wait([&systemConfiguration, &objServer, |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 910 | count](const boost::system::error_code& ec) { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 911 | if (ec == boost::asio::error::operation_aborted) |
| 912 | { |
| 913 | // we were cancelled |
| 914 | return; |
| 915 | } |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 916 | if (ec) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 917 | { |
| 918 | std::cerr << "async wait error " << ec << "\n"; |
| 919 | return; |
| 920 | } |
| 921 | |
James Feist | 2539ccd | 2020-05-01 16:15:08 -0700 | [diff] [blame] | 922 | if (inProgress) |
| 923 | { |
| 924 | propertiesChangedCallback(systemConfiguration, objServer); |
| 925 | return; |
| 926 | } |
| 927 | inProgress = true; |
| 928 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 929 | nlohmann::json oldConfiguration = systemConfiguration; |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 930 | auto missingConfigurations = std::make_shared<nlohmann::json>(); |
| 931 | *missingConfigurations = systemConfiguration; |
| 932 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 933 | std::list<nlohmann::json> configurations; |
| 934 | if (!findJsonFiles(configurations)) |
| 935 | { |
| 936 | std::cerr << "cannot find json files\n"; |
James Feist | 2539ccd | 2020-05-01 16:15:08 -0700 | [diff] [blame] | 937 | inProgress = false; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 938 | return; |
| 939 | } |
| 940 | |
| 941 | auto perfScan = std::make_shared<PerformScan>( |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 942 | systemConfiguration, *missingConfigurations, configurations, |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 943 | objServer, |
| 944 | [&systemConfiguration, &objServer, count, oldConfiguration, |
| 945 | missingConfigurations]() { |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 946 | // this is something that since ac has been applied to the bmc |
| 947 | // we saw, and we no longer see it |
| 948 | bool powerOff = !isPowerOn(); |
| 949 | for (const auto& item : missingConfigurations->items()) |
| 950 | { |
| 951 | bool isDetectedPowerOn = false; |
| 952 | auto powerState = item.value().find("PowerState"); |
| 953 | if (powerState != item.value().end()) |
| 954 | { |
| 955 | auto ptr = powerState->get_ptr<const std::string*>(); |
| 956 | if (ptr) |
| 957 | { |
| 958 | if (*ptr == "On" || *ptr == "BiosPost") |
| 959 | { |
| 960 | isDetectedPowerOn = true; |
| 961 | } |
| 962 | } |
| 963 | } |
| 964 | if (powerOff && isDetectedPowerOn) |
| 965 | { |
| 966 | // power not on yet, don't know if it's there or not |
| 967 | continue; |
| 968 | } |
| 969 | std::string name = item.value()["Name"].get<std::string>(); |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 970 | std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>& |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 971 | ifaces = inventory[name]; |
| 972 | for (auto& iface : ifaces) |
| 973 | { |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 974 | auto sharedPtr = iface.lock(); |
| 975 | if (!sharedPtr) |
| 976 | { |
| 977 | continue; // was already deleted elsewhere |
| 978 | } |
| 979 | objServer.remove_interface(sharedPtr); |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 980 | } |
| 981 | ifaces.clear(); |
| 982 | systemConfiguration.erase(item.key()); |
| 983 | logDeviceRemoved(item.value()); |
| 984 | } |
| 985 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 986 | nlohmann::json newConfiguration = systemConfiguration; |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 987 | for (auto it = newConfiguration.begin(); |
| 988 | it != newConfiguration.end();) |
| 989 | { |
| 990 | auto findKey = oldConfiguration.find(it.key()); |
| 991 | if (findKey != oldConfiguration.end()) |
| 992 | { |
| 993 | it = newConfiguration.erase(it); |
| 994 | } |
| 995 | else |
| 996 | { |
| 997 | it++; |
| 998 | } |
| 999 | } |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1000 | for (const auto& item : newConfiguration.items()) |
| 1001 | { |
| 1002 | logDeviceAdded(item.value()); |
| 1003 | } |
| 1004 | |
James Feist | 2539ccd | 2020-05-01 16:15:08 -0700 | [diff] [blame] | 1005 | inProgress = false; |
| 1006 | |
Jonathan Doman | 6d64982 | 2021-05-05 16:53:04 -0700 | [diff] [blame] | 1007 | io.post([count, newConfiguration, &systemConfiguration, |
| 1008 | &objServer]() { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1009 | loadOverlays(newConfiguration); |
James Feist | ce4367c | 2018-10-16 09:19:57 -0700 | [diff] [blame] | 1010 | |
Jonathan Doman | 6d64982 | 2021-05-05 16:53:04 -0700 | [diff] [blame] | 1011 | io.post([&systemConfiguration]() { |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 1012 | if (!writeJsonFiles(systemConfiguration)) |
| 1013 | { |
| 1014 | std::cerr << "Error writing json files\n"; |
| 1015 | } |
| 1016 | }); |
Jonathan Doman | 6d64982 | 2021-05-05 16:53:04 -0700 | [diff] [blame] | 1017 | io.post([count, newConfiguration, &systemConfiguration, |
| 1018 | &objServer]() { |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 1019 | postToDbus(newConfiguration, systemConfiguration, |
| 1020 | objServer); |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1021 | if (count != instance) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1022 | { |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1023 | return; |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1024 | } |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1025 | startRemovedTimer(timer, systemConfiguration); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1026 | }); |
| 1027 | }); |
| 1028 | }); |
| 1029 | perfScan->run(); |
| 1030 | }); |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1031 | } |
| 1032 | |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1033 | int main() |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1034 | { |
| 1035 | // setup connection to dbus |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1036 | systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 1037 | systemBus->request_name("xyz.openbmc_project.EntityManager"); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1038 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1039 | sdbusplus::asio::object_server objServer(systemBus); |
James Feist | fd1264a | 2018-05-03 12:10:00 -0700 | [diff] [blame] | 1040 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1041 | std::shared_ptr<sdbusplus::asio::dbus_interface> entityIface = |
| 1042 | objServer.add_interface("/xyz/openbmc_project/EntityManager", |
| 1043 | "xyz.openbmc_project.EntityManager"); |
James Feist | fd1264a | 2018-05-03 12:10:00 -0700 | [diff] [blame] | 1044 | |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1045 | // to keep reference to the match / filter objects so they don't get |
| 1046 | // destroyed |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1047 | |
| 1048 | nlohmann::json systemConfiguration = nlohmann::json::object(); |
| 1049 | |
Brad Bishop | c76af0f | 2020-12-04 13:50:23 -0500 | [diff] [blame] | 1050 | // We need a poke from DBus for static providers that create all their |
| 1051 | // objects prior to claiming a well-known name, and thus don't emit any |
| 1052 | // org.freedesktop.DBus.Properties signals. Similarly if a process exits |
| 1053 | // for any reason, expected or otherwise, we'll need a poke to remove |
| 1054 | // entities from DBus. |
| 1055 | sdbusplus::bus::match::match nameOwnerChangedMatch( |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1056 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
Brad Bishop | c76af0f | 2020-12-04 13:50:23 -0500 | [diff] [blame] | 1057 | sdbusplus::bus::match::rules::nameOwnerChanged(), |
| 1058 | [&](sdbusplus::message::message&) { |
| 1059 | propertiesChangedCallback(systemConfiguration, objServer); |
| 1060 | }); |
Brad Bishop | 10a8c5f | 2020-12-07 21:40:07 -0500 | [diff] [blame] | 1061 | // We also need a poke from DBus when new interfaces are created or |
| 1062 | // destroyed. |
| 1063 | sdbusplus::bus::match::match interfacesAddedMatch( |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1064 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
Brad Bishop | 10a8c5f | 2020-12-07 21:40:07 -0500 | [diff] [blame] | 1065 | sdbusplus::bus::match::rules::interfacesAdded(), |
| 1066 | [&](sdbusplus::message::message&) { |
| 1067 | propertiesChangedCallback(systemConfiguration, objServer); |
| 1068 | }); |
| 1069 | sdbusplus::bus::match::match interfacesRemovedMatch( |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1070 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
Brad Bishop | 10a8c5f | 2020-12-07 21:40:07 -0500 | [diff] [blame] | 1071 | sdbusplus::bus::match::rules::interfacesRemoved(), |
| 1072 | [&](sdbusplus::message::message&) { |
| 1073 | propertiesChangedCallback(systemConfiguration, objServer); |
| 1074 | }); |
Brad Bishop | c76af0f | 2020-12-04 13:50:23 -0500 | [diff] [blame] | 1075 | |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 1076 | io.post( |
| 1077 | [&]() { propertiesChangedCallback(systemConfiguration, objServer); }); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1078 | |
James Feist | fd1264a | 2018-05-03 12:10:00 -0700 | [diff] [blame] | 1079 | entityIface->register_method("ReScan", [&]() { |
James Feist | 4dc617b | 2020-05-01 09:54:47 -0700 | [diff] [blame] | 1080 | propertiesChangedCallback(systemConfiguration, objServer); |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1081 | }); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1082 | entityIface->initialize(); |
| 1083 | |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1084 | if (fwVersionIsSame()) |
| 1085 | { |
| 1086 | if (std::filesystem::is_regular_file(currentConfiguration)) |
| 1087 | { |
| 1088 | // this file could just be deleted, but it's nice for debug |
| 1089 | std::filesystem::create_directory(tempConfigDir); |
| 1090 | std::filesystem::remove(lastConfiguration); |
| 1091 | std::filesystem::copy(currentConfiguration, lastConfiguration); |
| 1092 | std::filesystem::remove(currentConfiguration); |
| 1093 | |
| 1094 | std::ifstream jsonStream(lastConfiguration); |
| 1095 | if (jsonStream.good()) |
| 1096 | { |
| 1097 | auto data = nlohmann::json::parse(jsonStream, nullptr, false); |
| 1098 | if (data.is_discarded()) |
| 1099 | { |
| 1100 | std::cerr << "syntax error in " << lastConfiguration |
| 1101 | << "\n"; |
| 1102 | } |
| 1103 | else |
| 1104 | { |
| 1105 | lastJson = std::move(data); |
| 1106 | } |
| 1107 | } |
| 1108 | else |
| 1109 | { |
| 1110 | std::cerr << "unable to open " << lastConfiguration << "\n"; |
| 1111 | } |
| 1112 | } |
| 1113 | } |
| 1114 | else |
| 1115 | { |
| 1116 | // not an error, just logging at this level to make it in the journal |
| 1117 | std::cerr << "Clearing previous configuration\n"; |
| 1118 | std::filesystem::remove(currentConfiguration); |
| 1119 | } |
| 1120 | |
| 1121 | // some boards only show up after power is on, we want to not say they are |
| 1122 | // removed until the same state happens |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 1123 | setupPowerMatch(systemBus); |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1124 | |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1125 | io.run(); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1126 | |
| 1127 | return 0; |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1128 | } |