| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 1 | #include "dbus_interface.hpp" |
| 2 | |
| 3 | #include "perform_probe.hpp" |
| Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 4 | #include "utils.hpp" |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 5 | |
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 6 | #include <phosphor-logging/lg2.hpp> |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 7 | |
| Ed Tanous | dbf95b2 | 2025-10-13 11:38:35 -0700 | [diff] [blame] | 8 | #include <flat_map> |
| Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 9 | #include <fstream> |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 10 | #include <regex> |
| 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 14 | namespace dbus_interface |
| 15 | { |
| 16 | |
| 17 | const std::regex illegalDbusPathRegex("[^A-Za-z0-9_.]"); |
| 18 | const std::regex illegalDbusMemberRegex("[^A-Za-z0-9_]"); |
| 19 | |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 20 | EMDBusInterface::EMDBusInterface(boost::asio::io_context& io, |
| 21 | sdbusplus::asio::object_server& objServer) : |
| 22 | io(io), objServer(objServer) |
| 23 | {} |
| 24 | |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 25 | void tryIfaceInitialize(std::shared_ptr<sdbusplus::asio::dbus_interface>& iface) |
| 26 | { |
| 27 | try |
| 28 | { |
| 29 | iface->initialize(); |
| 30 | } |
| 31 | catch (std::exception& e) |
| 32 | { |
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 33 | lg2::error( |
| 34 | "Unable to initialize dbus interface : {ERR} object Path : {PATH} interface name : {INTF}", |
| 35 | "ERR", e, "PATH", iface->get_object_path(), "INTF", |
| 36 | iface->get_interface_name()); |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
| Alexander Hansen | 57604ed | 2025-06-27 13:22:28 +0200 | [diff] [blame] | 40 | std::shared_ptr<sdbusplus::asio::dbus_interface> |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 41 | EMDBusInterface::createInterface(const std::string& path, |
| 42 | const std::string& interface, |
| 43 | const std::string& parent, bool checkNull) |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 44 | { |
| 45 | // on first add we have no reason to check for null before add, as there |
| 46 | // won't be any. For dynamically added interfaces, we check for null so that |
| 47 | // a constant delete/add will not create a memory leak |
| 48 | |
| 49 | auto ptr = objServer.add_interface(path, interface); |
| 50 | auto& dataVector = inventory[parent]; |
| 51 | if (checkNull) |
| 52 | { |
| 53 | auto it = std::find_if(dataVector.begin(), dataVector.end(), |
| 54 | [](const auto& p) { return p.expired(); }); |
| 55 | if (it != dataVector.end()) |
| 56 | { |
| 57 | *it = ptr; |
| 58 | return ptr; |
| 59 | } |
| 60 | } |
| 61 | dataVector.emplace_back(ptr); |
| 62 | return ptr; |
| 63 | } |
| 64 | |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 65 | void EMDBusInterface::createDeleteObjectMethod( |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 66 | const std::string& jsonPointerPath, |
| 67 | const std::shared_ptr<sdbusplus::asio::dbus_interface>& iface, |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 68 | nlohmann::json& systemConfiguration) |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 69 | { |
| 70 | std::weak_ptr<sdbusplus::asio::dbus_interface> interface = iface; |
| 71 | iface->register_method( |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 72 | "Delete", [this, &systemConfiguration, interface, |
| 73 | jsonPointerPath{std::string(jsonPointerPath)}]() { |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 74 | std::shared_ptr<sdbusplus::asio::dbus_interface> dbusInterface = |
| 75 | interface.lock(); |
| 76 | if (!dbusInterface) |
| 77 | { |
| 78 | // this technically can't happen as the pointer is pointing to |
| 79 | // us |
| 80 | throw DBusInternalError(); |
| 81 | } |
| 82 | nlohmann::json::json_pointer ptr(jsonPointerPath); |
| 83 | systemConfiguration[ptr] = nullptr; |
| 84 | |
| 85 | // todo(james): dig through sdbusplus to find out why we can't |
| 86 | // delete it in a method call |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 87 | boost::asio::post(io, [dbusInterface, this]() mutable { |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 88 | objServer.remove_interface(dbusInterface); |
| 89 | }); |
| 90 | |
| Christopher Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame] | 91 | if (!writeJsonFiles(systemConfiguration)) |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 92 | { |
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 93 | lg2::error("error setting json file"); |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 94 | throw DBusInternalError(); |
| 95 | } |
| 96 | }); |
| 97 | } |
| 98 | |
| Alexander Hansen | d72dc33 | 2025-06-10 10:49:24 +0200 | [diff] [blame] | 99 | static bool checkArrayElementsSameType(nlohmann::json& value) |
| 100 | { |
| 101 | nlohmann::json::array_t* arr = value.get_ptr<nlohmann::json::array_t*>(); |
| 102 | if (arr == nullptr) |
| 103 | { |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | if (arr->empty()) |
| 108 | { |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | nlohmann::json::value_t firstType = value[0].type(); |
| 113 | return std::ranges::all_of(value, [firstType](const nlohmann::json& el) { |
| 114 | return el.type() == firstType; |
| 115 | }); |
| 116 | } |
| 117 | |
| Alexander Hansen | 91e360e | 2025-06-10 12:21:30 +0200 | [diff] [blame] | 118 | static nlohmann::json::value_t getDBusType( |
| 119 | const nlohmann::json& value, nlohmann::json::value_t type, |
| Alexander Hansen | 17f8e6a | 2025-06-10 12:10:09 +0200 | [diff] [blame] | 120 | sdbusplus::asio::PropertyPermission permission) |
| 121 | { |
| 122 | const bool array = value.type() == nlohmann::json::value_t::array; |
| 123 | |
| 124 | if (permission == sdbusplus::asio::PropertyPermission::readWrite) |
| 125 | { |
| 126 | // all setable numbers are doubles as it is difficult to always |
| 127 | // create a configuration file with all whole numbers as decimals |
| 128 | // i.e. 1.0 |
| 129 | if (array) |
| 130 | { |
| 131 | if (value[0].is_number()) |
| 132 | { |
| Alexander Hansen | 91e360e | 2025-06-10 12:21:30 +0200 | [diff] [blame] | 133 | return nlohmann::json::value_t::number_float; |
| Alexander Hansen | 17f8e6a | 2025-06-10 12:10:09 +0200 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | else if (value.is_number()) |
| 137 | { |
| Alexander Hansen | 91e360e | 2025-06-10 12:21:30 +0200 | [diff] [blame] | 138 | return nlohmann::json::value_t::number_float; |
| Alexander Hansen | 17f8e6a | 2025-06-10 12:10:09 +0200 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| Alexander Hansen | 91e360e | 2025-06-10 12:21:30 +0200 | [diff] [blame] | 142 | return type; |
| 143 | } |
| 144 | |
| 145 | static void populateInterfacePropertyFromJson( |
| 146 | nlohmann::json& systemConfiguration, const std::string& path, |
| 147 | const nlohmann::json& key, const nlohmann::json& value, |
| 148 | nlohmann::json::value_t type, |
| 149 | std::shared_ptr<sdbusplus::asio::dbus_interface>& iface, |
| 150 | sdbusplus::asio::PropertyPermission permission) |
| 151 | { |
| 152 | const auto modifiedType = getDBusType(value, type, permission); |
| 153 | |
| 154 | switch (modifiedType) |
| Alexander Hansen | 17f8e6a | 2025-06-10 12:10:09 +0200 | [diff] [blame] | 155 | { |
| 156 | case (nlohmann::json::value_t::boolean): |
| 157 | { |
| Alexander Hansen | 5531eea | 2025-08-22 11:03:09 +0200 | [diff] [blame] | 158 | addValueToDBus<bool>(key, value, *iface, permission, |
| 159 | systemConfiguration, path); |
| Alexander Hansen | 17f8e6a | 2025-06-10 12:10:09 +0200 | [diff] [blame] | 160 | break; |
| 161 | } |
| 162 | case (nlohmann::json::value_t::number_integer): |
| 163 | { |
| 164 | addValueToDBus<int64_t>(key, value, *iface, permission, |
| 165 | systemConfiguration, path); |
| 166 | break; |
| 167 | } |
| 168 | case (nlohmann::json::value_t::number_unsigned): |
| 169 | { |
| 170 | addValueToDBus<uint64_t>(key, value, *iface, permission, |
| 171 | systemConfiguration, path); |
| 172 | break; |
| 173 | } |
| 174 | case (nlohmann::json::value_t::number_float): |
| 175 | { |
| 176 | addValueToDBus<double>(key, value, *iface, permission, |
| 177 | systemConfiguration, path); |
| 178 | break; |
| 179 | } |
| 180 | case (nlohmann::json::value_t::string): |
| 181 | { |
| 182 | addValueToDBus<std::string>(key, value, *iface, permission, |
| 183 | systemConfiguration, path); |
| 184 | break; |
| 185 | } |
| 186 | default: |
| 187 | { |
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 188 | lg2::error( |
| 189 | "Unexpected json type in system configuration {KEY}: {VALUE}", |
| 190 | "KEY", key, "VALUE", value.type_name()); |
| Alexander Hansen | 17f8e6a | 2025-06-10 12:10:09 +0200 | [diff] [blame] | 191 | break; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 196 | // adds simple json types to interface's properties |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 197 | void EMDBusInterface::populateInterfaceFromJson( |
| 198 | nlohmann::json& systemConfiguration, const std::string& jsonPointerPath, |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 199 | std::shared_ptr<sdbusplus::asio::dbus_interface>& iface, |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 200 | nlohmann::json& dict, sdbusplus::asio::PropertyPermission permission) |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 201 | { |
| 202 | for (const auto& [key, value] : dict.items()) |
| 203 | { |
| 204 | auto type = value.type(); |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 205 | if (value.type() == nlohmann::json::value_t::array) |
| 206 | { |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 207 | if (value.empty()) |
| 208 | { |
| 209 | continue; |
| 210 | } |
| 211 | type = value[0].type(); |
| Alexander Hansen | d72dc33 | 2025-06-10 10:49:24 +0200 | [diff] [blame] | 212 | if (!checkArrayElementsSameType(value)) |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 213 | { |
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 214 | lg2::error("dbus format error {VALUE}", "VALUE", value); |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 215 | continue; |
| 216 | } |
| 217 | } |
| 218 | if (type == nlohmann::json::value_t::object) |
| 219 | { |
| 220 | continue; // handled elsewhere |
| 221 | } |
| 222 | |
| 223 | std::string path = jsonPointerPath; |
| 224 | path.append("/").append(key); |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 225 | |
| Alexander Hansen | 17f8e6a | 2025-06-10 12:10:09 +0200 | [diff] [blame] | 226 | populateInterfacePropertyFromJson(systemConfiguration, path, key, value, |
| 227 | type, iface, permission); |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 228 | } |
| 229 | if (permission == sdbusplus::asio::PropertyPermission::readWrite) |
| 230 | { |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 231 | createDeleteObjectMethod(jsonPointerPath, iface, systemConfiguration); |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 232 | } |
| 233 | tryIfaceInitialize(iface); |
| 234 | } |
| 235 | |
| Alexander Hansen | f9a4024 | 2025-10-14 15:34:47 +0200 | [diff] [blame] | 236 | // @brief: throws on error |
| 237 | static void addObjectRuntimeValidateJson(const nlohmann::json& newData, |
| 238 | const std::string* type) |
| 239 | { |
| 240 | if constexpr (!ENABLE_RUNTIME_VALIDATE_JSON) |
| 241 | { |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | const std::filesystem::path schemaPath = |
| 246 | std::filesystem::path(schemaDirectory) / "exposes_record.json"; |
| 247 | |
| 248 | std::ifstream schemaFile{schemaPath}; |
| 249 | |
| 250 | if (!schemaFile.good()) |
| 251 | { |
| 252 | throw std::invalid_argument("No schema avaliable, cannot validate."); |
| 253 | } |
| 254 | nlohmann::json schema = |
| 255 | nlohmann::json::parse(schemaFile, nullptr, false, true); |
| 256 | if (schema.is_discarded()) |
| 257 | { |
| 258 | lg2::error("Schema not legal: {TYPE}.json", "TYPE", *type); |
| 259 | throw DBusInternalError(); |
| 260 | } |
| 261 | |
| 262 | if (!validateJson(schema, newData)) |
| 263 | { |
| 264 | throw std::invalid_argument("Data does not match schema"); |
| 265 | } |
| 266 | } |
| 267 | |
| Alexander Hansen | a182cb7 | 2025-10-14 15:22:34 +0200 | [diff] [blame] | 268 | void EMDBusInterface::addObject( |
| 269 | const std::flat_map<std::string, JsonVariantType, std::less<>>& data, |
| 270 | nlohmann::json& systemConfiguration, const std::string& jsonPointerPath, |
| 271 | const std::string& path, const std::string& board) |
| 272 | { |
| 273 | nlohmann::json::json_pointer ptr(jsonPointerPath); |
| 274 | nlohmann::json& base = systemConfiguration[ptr]; |
| 275 | auto findExposes = base.find("Exposes"); |
| 276 | |
| 277 | if (findExposes == base.end()) |
| 278 | { |
| 279 | throw std::invalid_argument("Entity must have children."); |
| 280 | } |
| 281 | |
| 282 | // this will throw invalid-argument to sdbusplus if invalid json |
| 283 | nlohmann::json newData{}; |
| 284 | for (const auto& item : data) |
| 285 | { |
| 286 | nlohmann::json& newJson = newData[item.first]; |
| 287 | std::visit( |
| 288 | [&newJson](auto&& val) { |
| 289 | newJson = std::forward<decltype(val)>(val); |
| 290 | }, |
| 291 | item.second); |
| 292 | } |
| 293 | |
| Alexander Hansen | 7d5f205 | 2025-10-14 15:57:24 +0200 | [diff] [blame^] | 294 | addObjectJson(newData, systemConfiguration, jsonPointerPath, path, board); |
| 295 | } |
| 296 | |
| 297 | void EMDBusInterface::addObjectJson( |
| 298 | nlohmann::json& newData, nlohmann::json& systemConfiguration, |
| 299 | const std::string& jsonPointerPath, const std::string& path, |
| 300 | const std::string& board) |
| 301 | { |
| 302 | nlohmann::json::json_pointer ptr(jsonPointerPath); |
| 303 | nlohmann::json& base = systemConfiguration[ptr]; |
| 304 | auto findExposes = base.find("Exposes"); |
| Alexander Hansen | a182cb7 | 2025-10-14 15:22:34 +0200 | [diff] [blame] | 305 | auto findName = newData.find("Name"); |
| 306 | auto findType = newData.find("Type"); |
| 307 | if (findName == newData.end() || findType == newData.end()) |
| 308 | { |
| 309 | throw std::invalid_argument("AddObject missing Name or Type"); |
| 310 | } |
| 311 | const std::string* type = findType->get_ptr<const std::string*>(); |
| 312 | const std::string* name = findName->get_ptr<const std::string*>(); |
| 313 | if (type == nullptr || name == nullptr) |
| 314 | { |
| 315 | throw std::invalid_argument("Type and Name must be a string."); |
| 316 | } |
| 317 | |
| 318 | bool foundNull = false; |
| 319 | size_t lastIndex = 0; |
| 320 | // we add in the "exposes" |
| 321 | for (const auto& expose : *findExposes) |
| 322 | { |
| 323 | if (expose.is_null()) |
| 324 | { |
| 325 | foundNull = true; |
| 326 | continue; |
| 327 | } |
| 328 | |
| 329 | if (expose["Name"] == *name && expose["Type"] == *type) |
| 330 | { |
| 331 | throw std::invalid_argument("Field already in JSON, not adding"); |
| 332 | } |
| 333 | |
| 334 | if (foundNull) |
| 335 | { |
| 336 | continue; |
| 337 | } |
| 338 | |
| 339 | lastIndex++; |
| 340 | } |
| 341 | |
| Alexander Hansen | f9a4024 | 2025-10-14 15:34:47 +0200 | [diff] [blame] | 342 | addObjectRuntimeValidateJson(newData, type); |
| Alexander Hansen | a182cb7 | 2025-10-14 15:22:34 +0200 | [diff] [blame] | 343 | |
| 344 | if (foundNull) |
| 345 | { |
| 346 | findExposes->at(lastIndex) = newData; |
| 347 | } |
| 348 | else |
| 349 | { |
| 350 | findExposes->push_back(newData); |
| 351 | } |
| 352 | if (!writeJsonFiles(systemConfiguration)) |
| 353 | { |
| 354 | lg2::error("Error writing json files"); |
| 355 | } |
| 356 | std::string dbusName = *name; |
| 357 | |
| 358 | std::regex_replace(dbusName.begin(), dbusName.begin(), dbusName.end(), |
| 359 | illegalDbusMemberRegex, "_"); |
| 360 | |
| 361 | std::shared_ptr<sdbusplus::asio::dbus_interface> interface = |
| 362 | createInterface(path + "/" + dbusName, |
| 363 | "xyz.openbmc_project.Configuration." + *type, board, |
| 364 | true); |
| 365 | // permission is read-write, as since we just created it, must be |
| 366 | // runtime modifiable |
| 367 | populateInterfaceFromJson( |
| 368 | systemConfiguration, |
| 369 | jsonPointerPath + "/Exposes/" + std::to_string(lastIndex), interface, |
| 370 | newData, sdbusplus::asio::PropertyPermission::readWrite); |
| 371 | } |
| 372 | |
| Alexander Hansen | 57604ed | 2025-06-27 13:22:28 +0200 | [diff] [blame] | 373 | void EMDBusInterface::createAddObjectMethod( |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 374 | const std::string& jsonPointerPath, const std::string& path, |
| 375 | nlohmann::json& systemConfiguration, const std::string& board) |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 376 | { |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 377 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
| 378 | createInterface(path, "xyz.openbmc_project.AddObject", board); |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 379 | |
| 380 | iface->register_method( |
| 381 | "AddObject", |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 382 | [&systemConfiguration, jsonPointerPath{std::string(jsonPointerPath)}, |
| Alexander Hansen | a182cb7 | 2025-10-14 15:22:34 +0200 | [diff] [blame] | 383 | path{std::string(path)}, board{std::string(board)}, |
| Ed Tanous | dbf95b2 | 2025-10-13 11:38:35 -0700 | [diff] [blame] | 384 | this](const std::flat_map<std::string, JsonVariantType, std::less<>>& |
| Alexander Hansen | 57604ed | 2025-06-27 13:22:28 +0200 | [diff] [blame] | 385 | data) { |
| Alexander Hansen | a182cb7 | 2025-10-14 15:22:34 +0200 | [diff] [blame] | 386 | addObject(data, systemConfiguration, jsonPointerPath, path, board); |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 387 | }); |
| 388 | tryIfaceInitialize(iface); |
| 389 | } |
| 390 | |
| 391 | std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>& |
| Alexander Hansen | 57604ed | 2025-06-27 13:22:28 +0200 | [diff] [blame] | 392 | EMDBusInterface::getDeviceInterfaces(const nlohmann::json& device) |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 393 | { |
| 394 | return inventory[device["Name"].get<std::string>()]; |
| 395 | } |
| 396 | |
| 397 | } // namespace dbus_interface |