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