John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 1 | |
| 2 | #include "getConfig.hpp" |
| 3 | |
John Wedig | 3cf9e80 | 2024-04-30 14:13:05 -0700 | [diff] [blame^] | 4 | #include <boost/asio/steady_timer.hpp> |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 5 | #include <phosphor-logging/lg2.hpp> |
| 6 | #include <sdbusplus/asio/connection.hpp> |
| 7 | |
John Wedig | 3cf9e80 | 2024-04-30 14:13:05 -0700 | [diff] [blame^] | 8 | #include <chrono> |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 9 | #include <cstdlib> |
| 10 | #include <memory> |
| 11 | #include <string> |
| 12 | #include <utility> |
| 13 | #include <variant> |
| 14 | #include <vector> |
| 15 | |
| 16 | namespace estoraged |
| 17 | { |
| 18 | |
| 19 | namespace mapper |
| 20 | { |
| 21 | constexpr const char* busName = "xyz.openbmc_project.ObjectMapper"; |
| 22 | constexpr const char* path = "/xyz/openbmc_project/object_mapper"; |
| 23 | constexpr const char* interface = "xyz.openbmc_project.ObjectMapper"; |
| 24 | constexpr const char* subtree = "GetSubTree"; |
| 25 | } // namespace mapper |
| 26 | |
| 27 | using GetSubTreeType = std::vector< |
| 28 | std::pair<std::string, |
| 29 | std::vector<std::pair<std::string, std::vector<std::string>>>>>; |
| 30 | |
| 31 | void GetStorageConfiguration::getStorageInfo(const std::string& path, |
John Wedig | 3cf9e80 | 2024-04-30 14:13:05 -0700 | [diff] [blame^] | 32 | const std::string& owner, |
| 33 | size_t retries) |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 34 | { |
| 35 | std::shared_ptr<GetStorageConfiguration> self = shared_from_this(); |
| 36 | self->dbusConnection->async_method_call( |
John Wedig | 3cf9e80 | 2024-04-30 14:13:05 -0700 | [diff] [blame^] | 37 | [self, path, owner, retries]( |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 38 | const boost::system::error_code ec, |
| 39 | boost::container::flat_map<std::string, BasicVariantType>& data) { |
Patrick Williams | 04c28fa | 2023-05-10 07:51:24 -0500 | [diff] [blame] | 40 | if (ec) |
| 41 | { |
John Wedig | 3cf9e80 | 2024-04-30 14:13:05 -0700 | [diff] [blame^] | 42 | lg2::error( |
| 43 | "Error getting properties for {PATH}: {RETRIES} retries left", |
| 44 | "PATH", path, "RETRIES", retries - 1, "REDFISH_MESSAGE_ID", |
| 45 | std::string("OpenBMC.0.1.GetStorageInfoFail")); |
| 46 | if (retries == 0U) |
| 47 | { |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | auto timer = std::make_shared<boost::asio::steady_timer>( |
| 52 | self->dbusConnection->get_io_context()); |
| 53 | timer->expires_after(std::chrono::seconds(10)); |
| 54 | timer->async_wait([self, timer, path, owner, |
| 55 | retries](boost::system::error_code ec) { |
| 56 | if (ec) |
| 57 | { |
| 58 | lg2::error("Timer error!"); |
| 59 | return; |
| 60 | } |
| 61 | self->getStorageInfo(path, owner, retries - 1); |
| 62 | }); |
| 63 | |
Patrick Williams | 04c28fa | 2023-05-10 07:51:24 -0500 | [diff] [blame] | 64 | return; |
| 65 | } |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 66 | |
Patrick Williams | 04c28fa | 2023-05-10 07:51:24 -0500 | [diff] [blame] | 67 | self->respData[path] = std::move(data); |
Patrick Williams | ff1b64f | 2023-10-20 11:19:56 -0500 | [diff] [blame] | 68 | }, |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 69 | owner, path, "org.freedesktop.DBus.Properties", "GetAll", |
| 70 | emmcConfigInterface); |
| 71 | } |
| 72 | |
| 73 | void GetStorageConfiguration::getConfiguration() |
| 74 | { |
| 75 | std::shared_ptr<GetStorageConfiguration> self = shared_from_this(); |
| 76 | dbusConnection->async_method_call( |
| 77 | [self](const boost::system::error_code ec, const GetSubTreeType& ret) { |
Patrick Williams | 04c28fa | 2023-05-10 07:51:24 -0500 | [diff] [blame] | 78 | if (ec) |
| 79 | { |
| 80 | lg2::error("Error calling mapper"); |
| 81 | return; |
| 82 | } |
| 83 | for (const auto& [objPath, objDict] : ret) |
| 84 | { |
| 85 | if (objDict.empty()) |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 86 | { |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 87 | return; |
| 88 | } |
Patrick Williams | 04c28fa | 2023-05-10 07:51:24 -0500 | [diff] [blame] | 89 | const std::string& objOwner = objDict.begin()->first; |
| 90 | /* Look for the config interface exposed by this object. */ |
| 91 | for (const std::string& interface : objDict.begin()->second) |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 92 | { |
Patrick Williams | 04c28fa | 2023-05-10 07:51:24 -0500 | [diff] [blame] | 93 | if (interface.compare(emmcConfigInterface) == 0) |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 94 | { |
Patrick Williams | 04c28fa | 2023-05-10 07:51:24 -0500 | [diff] [blame] | 95 | /* Get the properties exposed by this interface. */ |
| 96 | self->getStorageInfo(objPath, objOwner); |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
Patrick Williams | 04c28fa | 2023-05-10 07:51:24 -0500 | [diff] [blame] | 99 | } |
Patrick Williams | ff1b64f | 2023-10-20 11:19:56 -0500 | [diff] [blame] | 100 | }, |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 101 | mapper::busName, mapper::path, mapper::interface, mapper::subtree, "/", |
| 102 | 0, std::vector<const char*>(1, emmcConfigInterface)); |
| 103 | } |
| 104 | |
| 105 | GetStorageConfiguration::~GetStorageConfiguration() |
| 106 | { |
| 107 | callback(respData); |
| 108 | } |
| 109 | |
| 110 | } // namespace estoraged |