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