blob: 3d2cc5accd2f3f5b75e27e0ee51b8ccb98795fb4 [file] [log] [blame]
John Wedigd32b9662022-04-13 18:12:25 -07001
2#include "getConfig.hpp"
3
John Wedig3cf9e802024-04-30 14:13:05 -07004#include <boost/asio/steady_timer.hpp>
John Wedigd32b9662022-04-13 18:12:25 -07005#include <phosphor-logging/lg2.hpp>
6#include <sdbusplus/asio/connection.hpp>
7
John Wedig3cf9e802024-04-30 14:13:05 -07008#include <chrono>
John Wedigd32b9662022-04-13 18:12:25 -07009#include <cstdlib>
10#include <memory>
11#include <string>
12#include <utility>
13#include <variant>
14#include <vector>
15
16namespace estoraged
17{
18
19namespace mapper
20{
21constexpr const char* busName = "xyz.openbmc_project.ObjectMapper";
22constexpr const char* path = "/xyz/openbmc_project/object_mapper";
23constexpr const char* interface = "xyz.openbmc_project.ObjectMapper";
24constexpr const char* subtree = "GetSubTree";
25} // namespace mapper
26
27using GetSubTreeType = std::vector<
28 std::pair<std::string,
29 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
30
Patrick Williams15b63e12024-08-16 15:22:01 -040031void GetStorageConfiguration::getStorageInfo(
32 const std::string& path, const std::string& owner, size_t retries)
John Wedigd32b9662022-04-13 18:12:25 -070033{
34 std::shared_ptr<GetStorageConfiguration> self = shared_from_this();
35 self->dbusConnection->async_method_call(
John Wedig3cf9e802024-04-30 14:13:05 -070036 [self, path, owner, retries](
John Wedigd32b9662022-04-13 18:12:25 -070037 const boost::system::error_code ec,
38 boost::container::flat_map<std::string, BasicVariantType>& data) {
Patrick Williams15b63e12024-08-16 15:22:01 -040039 if (ec)
John Wedig3cf9e802024-04-30 14:13:05 -070040 {
Patrick Williams15b63e12024-08-16 15:22:01 -040041 lg2::error(
42 "Error getting properties for {PATH}: {RETRIES} retries left",
43 "PATH", path, "RETRIES", retries - 1, "REDFISH_MESSAGE_ID",
44 std::string("OpenBMC.0.1.GetStorageInfoFail"));
45 if (retries == 0U)
46 {
47 return;
48 }
49
50 auto timer = std::make_shared<boost::asio::steady_timer>(
51 self->dbusConnection->get_io_context());
52 timer->expires_after(std::chrono::seconds(10));
53 timer->async_wait([self, timer, path, owner,
54 retries](boost::system::error_code ec) {
55 if (ec)
56 {
57 lg2::error("Timer error!");
58 return;
59 }
60 self->getStorageInfo(path, owner, retries - 1);
61 });
62
John Wedig3cf9e802024-04-30 14:13:05 -070063 return;
64 }
65
Patrick Williams15b63e12024-08-16 15:22:01 -040066 self->respData[path] = std::move(data);
67 },
John Wedigd32b9662022-04-13 18:12:25 -070068 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
69 emmcConfigInterface);
70}
71
72void GetStorageConfiguration::getConfiguration()
73{
74 std::shared_ptr<GetStorageConfiguration> self = shared_from_this();
75 dbusConnection->async_method_call(
76 [self](const boost::system::error_code ec, const GetSubTreeType& ret) {
Patrick Williams15b63e12024-08-16 15:22:01 -040077 if (ec)
John Wedigd32b9662022-04-13 18:12:25 -070078 {
Patrick Williams15b63e12024-08-16 15:22:01 -040079 lg2::error("Error calling mapper");
John Wedigd32b9662022-04-13 18:12:25 -070080 return;
81 }
Patrick Williams15b63e12024-08-16 15:22:01 -040082 for (const auto& [objPath, objDict] : ret)
John Wedigd32b9662022-04-13 18:12:25 -070083 {
Patrick Williams15b63e12024-08-16 15:22:01 -040084 if (objDict.empty())
John Wedigd32b9662022-04-13 18:12:25 -070085 {
Patrick Williams15b63e12024-08-16 15:22:01 -040086 return;
87 }
88 const std::string& objOwner = objDict.begin()->first;
89 /* Look for the config interface exposed by this object. */
90 for (const std::string& interface : objDict.begin()->second)
91 {
92 if (interface.compare(emmcConfigInterface) == 0)
93 {
94 /* Get the properties exposed by this interface. */
95 self->getStorageInfo(objPath, objOwner);
96 }
John Wedigd32b9662022-04-13 18:12:25 -070097 }
98 }
Patrick Williams15b63e12024-08-16 15:22:01 -040099 },
John Wedigd32b9662022-04-13 18:12:25 -0700100 mapper::busName, mapper::path, mapper::interface, mapper::subtree, "/",
101 0, std::vector<const char*>(1, emmcConfigInterface));
102}
103
104GetStorageConfiguration::~GetStorageConfiguration()
105{
106 callback(respData);
107}
108
109} // namespace estoraged