blob: d278724b437372c922b195a41028733a5cc1e2e9 [file] [log] [blame]
John Wedig2098dab2021-09-14 13:56:28 -07001
2#include "estoraged.hpp"
John Wedigd32b9662022-04-13 18:12:25 -07003#include "getConfig.hpp"
4#include "util.hpp"
John Wedig2098dab2021-09-14 13:56:28 -07005
John Wedigd32b9662022-04-13 18:12:25 -07006#include <boost/asio/deadline_timer.hpp>
John Wedig67a47442022-04-05 17:21:29 -07007#include <boost/asio/io_context.hpp>
John Wedigd32b9662022-04-13 18:12:25 -07008#include <boost/asio/post.hpp>
9#include <boost/container/flat_map.hpp>
10#include <boost/container/throw_exception.hpp>
John Edward Broadbent4e13b0a2021-11-15 15:21:59 -080011#include <phosphor-logging/lg2.hpp>
John Wedig67a47442022-04-05 17:21:29 -070012#include <sdbusplus/asio/connection.hpp>
13#include <sdbusplus/asio/object_server.hpp>
John Wedig2098dab2021-09-14 13:56:28 -070014#include <sdbusplus/bus.hpp>
John Wedigd32b9662022-04-13 18:12:25 -070015#include <sdbusplus/bus/match.hpp>
John Edward Broadbente35e7362022-03-22 16:14:24 -070016#include <util.hpp>
John Wedig2098dab2021-09-14 13:56:28 -070017
John Wedigd32b9662022-04-13 18:12:25 -070018#include <cstdlib>
John Wedig2098dab2021-09-14 13:56:28 -070019#include <filesystem>
20#include <iostream>
John Wedig67a47442022-04-05 17:21:29 -070021#include <memory>
John Wedig2098dab2021-09-14 13:56:28 -070022#include <string>
23
John Wedigd32b9662022-04-13 18:12:25 -070024/*
25 * Get the configuration objects from Entity Manager and create new D-Bus
26 * objects for each one. This function can be called multiple times, in case
27 * new configuration objects show up later.
28 *
29 * Note: Currently, eStoraged can only support 1 eMMC device.
30 * Additional changes will be needed to support more than 1 eMMC, or to support
31 * more types of storage devices.
32 */
33void createStorageObjects(
John Wedig61cf4262023-03-17 14:32:04 -070034 sdbusplus::asio::object_server& objectServer,
John Wedigd32b9662022-04-13 18:12:25 -070035 boost::container::flat_map<
36 std::string, std::unique_ptr<estoraged::EStoraged>>& storageObjects,
37 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
John Wedig2098dab2021-09-14 13:56:28 -070038{
John Wedigd32b9662022-04-13 18:12:25 -070039 auto getter = std::make_shared<estoraged::GetStorageConfiguration>(
40 dbusConnection,
John Wedig61cf4262023-03-17 14:32:04 -070041 [&objectServer, &storageObjects](
John Wedigd32b9662022-04-13 18:12:25 -070042 const estoraged::ManagedStorageType& storageConfigurations) {
Patrick Williams04c28fa2023-05-10 07:51:24 -050043 size_t numConfigObj = storageConfigurations.size();
44 if (numConfigObj > 1)
45 {
46 lg2::error("eStoraged can only manage 1 eMMC device; found {NUM}",
47 "NUM", numConfigObj, "REDFISH_MESSAGE_ID",
48 std::string("OpenBMC.0.1.CreateStorageObjectsFail"));
49 return;
50 }
51
52 for (const std::pair<sdbusplus::message::object_path,
53 estoraged::StorageData>& storage :
54 storageConfigurations)
55 {
56 const std::string& path = storage.first.str;
57
58 if (storageObjects.find(path) != storageObjects.end())
John Wedigd32b9662022-04-13 18:12:25 -070059 {
Patrick Williams04c28fa2023-05-10 07:51:24 -050060 /*
61 * We've already created this object, or at least
62 * attempted to.
63 */
64 continue;
John Wedigd32b9662022-04-13 18:12:25 -070065 }
66
Patrick Williams04c28fa2023-05-10 07:51:24 -050067 /* Get the properties from the config object. */
68 const estoraged::StorageData& data = storage.second;
69
70 /* Look for the device file. */
71 const std::filesystem::path blockDevDir{"/sys/block"};
72 std::filesystem::path deviceFile, sysfsDir;
Rahul Kapoor19825052023-05-27 01:52:23 +000073 std::string luksName, locationCode;
74 bool found = estoraged::util::findDevice(data, blockDevDir,
75 deviceFile, sysfsDir,
76 luksName, locationCode);
Patrick Williams04c28fa2023-05-10 07:51:24 -050077 if (!found)
John Wedigd32b9662022-04-13 18:12:25 -070078 {
Patrick Williams04c28fa2023-05-10 07:51:24 -050079 lg2::error("Device not found for path {PATH}", "PATH", path,
80 "REDFISH_MESSAGE_ID",
81 std::string("OpenBMC.0.1.CreateStorageObjectsFail"));
82 /*
83 * Set a NULL pointer as a placeholder, so that we don't
84 * try and fail again later.
85 */
86 storageObjects[path] = nullptr;
87 continue;
John Wedigd32b9662022-04-13 18:12:25 -070088 }
Patrick Williams04c28fa2023-05-10 07:51:24 -050089
90 uint64_t size = estoraged::util::findSizeOfBlockDevice(deviceFile);
91
92 uint8_t lifeleft =
93 estoraged::util::findPredictedMediaLifeLeftPercent(sysfsDir);
94 std::string partNumber = estoraged::util::getPartNumber(sysfsDir);
95 std::string serialNumber =
96 estoraged::util::getSerialNumber(sysfsDir);
97 /* Create the storage object. */
98 storageObjects[path] = std::make_unique<estoraged::EStoraged>(
99 objectServer, path, deviceFile, luksName, size, lifeleft,
Rahul Kapoor19825052023-05-27 01:52:23 +0000100 partNumber, serialNumber, locationCode);
Patrick Williams04c28fa2023-05-10 07:51:24 -0500101 lg2::info("Created eStoraged object for path {PATH}", "PATH", path,
102 "REDFISH_MESSAGE_ID",
103 std::string("OpenBMC.0.1.CreateStorageObjects"));
104 }
John Wedigd32b9662022-04-13 18:12:25 -0700105 });
106 getter->getConfiguration();
John Wedig2098dab2021-09-14 13:56:28 -0700107}
108
John Wedigd32b9662022-04-13 18:12:25 -0700109int main(void)
John Wedig2098dab2021-09-14 13:56:28 -0700110{
Ed Tanous82897c32022-02-21 14:11:59 -0800111 try
John Wedig2098dab2021-09-14 13:56:28 -0700112 {
John Wedig67a47442022-04-05 17:21:29 -0700113 // setup connection to dbus
114 boost::asio::io_context io;
115 auto conn = std::make_shared<sdbusplus::asio::connection>(io);
116 // request D-Bus server name.
John Wedigd32b9662022-04-13 18:12:25 -0700117 conn->request_name("xyz.openbmc_project.eStoraged");
118 sdbusplus::asio::object_server server(conn);
119 boost::container::flat_map<std::string,
120 std::unique_ptr<estoraged::EStoraged>>
121 storageObjects;
Ed Tanous82897c32022-02-21 14:11:59 -0800122
John Wedig61cf4262023-03-17 14:32:04 -0700123 boost::asio::post(
124 io, [&]() { createStorageObjects(server, storageObjects, conn); });
John Wedigd32b9662022-04-13 18:12:25 -0700125
126 /*
127 * Set up an event handler to process any new configuration objects
128 * that show up later.
129 */
130 boost::asio::deadline_timer filterTimer(io);
Patrick Williamse3ef7652022-07-22 19:26:57 -0500131 std::function<void(sdbusplus::message_t&)> eventHandler =
132 [&](sdbusplus::message_t& message) {
Patrick Williams04c28fa2023-05-10 07:51:24 -0500133 if (message.is_method_error())
134 {
135 lg2::error("eventHandler callback method error");
136 return;
137 }
138 /*
139 * This implicitly cancels the timer, if it's already pending.
140 * If there's a burst of events within a short period, we want
141 * to handle them all at once. So, we will wait this long for no
142 * more events to occur, before processing them.
143 */
144 filterTimer.expires_from_now(boost::posix_time::seconds(1));
145
146 filterTimer.async_wait([&](const boost::system::error_code& ec) {
147 if (ec == boost::asio::error::operation_aborted)
John Wedigd32b9662022-04-13 18:12:25 -0700148 {
Patrick Williams04c28fa2023-05-10 07:51:24 -0500149 /* we were canceled */
John Wedigd32b9662022-04-13 18:12:25 -0700150 return;
151 }
Patrick Williams04c28fa2023-05-10 07:51:24 -0500152 if (ec)
153 {
154 lg2::error("timer error");
155 return;
156 }
157 createStorageObjects(server, storageObjects, conn);
158 });
159 };
John Wedigd32b9662022-04-13 18:12:25 -0700160
Patrick Williamse3ef7652022-07-22 19:26:57 -0500161 auto match = std::make_unique<sdbusplus::bus::match_t>(
162 static_cast<sdbusplus::bus_t&>(*conn),
John Wedigd32b9662022-04-13 18:12:25 -0700163 "type='signal',member='PropertiesChanged',path_namespace='" +
164 std::string("/xyz/openbmc_project/inventory") +
165 "',arg0namespace='" + estoraged::emmcConfigInterface + "'",
166 eventHandler);
167
Ed Tanous82897c32022-02-21 14:11:59 -0800168 lg2::info("Storage management service is running", "REDFISH_MESSAGE_ID",
169 std::string("OpenBMC.1.0.ServiceStarted"));
170
John Wedig67a47442022-04-05 17:21:29 -0700171 io.run();
172 return 0;
John Wedig2098dab2021-09-14 13:56:28 -0700173 }
Ed Tanous82897c32022-02-21 14:11:59 -0800174 catch (const std::exception& e)
John Wedig2098dab2021-09-14 13:56:28 -0700175 {
Ed Tanous82897c32022-02-21 14:11:59 -0800176 lg2::error(e.what(), "REDFISH_MESSAGE_ID",
177 std::string("OpenBMC.1.0.ServiceException"));
John Wedig2098dab2021-09-14 13:56:28 -0700178
Ed Tanous82897c32022-02-21 14:11:59 -0800179 return 2;
180 }
John Wedig2098dab2021-09-14 13:56:28 -0700181 return 1;
182}