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