xref: /openbmc/phosphor-led-manager/fault-monitor/fru-fault-monitor.cpp (revision d78de14df5a9ee6babb7f4ef75a1c4b58bf6376c)
191ac8d3aSPatrick Venture #include "fru-fault-monitor.hpp"
291ac8d3aSPatrick Venture 
391ac8d3aSPatrick Venture #include "elog-errors.hpp"
491ac8d3aSPatrick Venture 
591ac8d3aSPatrick Venture #include <phosphor-logging/elog.hpp>
691ac8d3aSPatrick Venture #include <sdbusplus/exception.hpp>
74b062010SGeorge Liu #include <xyz/openbmc_project/Led/Fru/Monitor/error.hpp>
84b062010SGeorge Liu #include <xyz/openbmc_project/Led/Mapper/error.hpp>
93c6f29a0SDhruvaraj Subhashchandran 
1059b86cd7SDhruvaraj Subhashchandran namespace phosphor
1159b86cd7SDhruvaraj Subhashchandran {
1259b86cd7SDhruvaraj Subhashchandran namespace led
1359b86cd7SDhruvaraj Subhashchandran {
1459b86cd7SDhruvaraj Subhashchandran namespace fru
1559b86cd7SDhruvaraj Subhashchandran {
1659b86cd7SDhruvaraj Subhashchandran namespace fault
1759b86cd7SDhruvaraj Subhashchandran {
1859b86cd7SDhruvaraj Subhashchandran namespace monitor
1959b86cd7SDhruvaraj Subhashchandran {
2059b86cd7SDhruvaraj Subhashchandran 
213c6f29a0SDhruvaraj Subhashchandran using namespace phosphor::logging;
223c6f29a0SDhruvaraj Subhashchandran 
233c6f29a0SDhruvaraj Subhashchandran constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
243c6f29a0SDhruvaraj Subhashchandran constexpr auto MAPPER_OBJ_PATH = "/xyz/openbmc_project/object_mapper";
253c6f29a0SDhruvaraj Subhashchandran constexpr auto MAPPER_IFACE = "xyz.openbmc_project.ObjectMapper";
263c6f29a0SDhruvaraj Subhashchandran constexpr auto OBJMGR_IFACE = "org.freedesktop.DBus.ObjectManager";
273c6f29a0SDhruvaraj Subhashchandran constexpr auto LED_GROUPS = "/xyz/openbmc_project/led/groups/";
283c6f29a0SDhruvaraj Subhashchandran constexpr auto LOG_PATH = "/xyz/openbmc_project/logging";
29891c4769SDhruvaraj Subhashchandran constexpr auto LOG_IFACE = "xyz.openbmc_project.Logging.Entry";
303c6f29a0SDhruvaraj Subhashchandran 
3191ac8d3aSPatrick Venture using AssociationList =
3291ac8d3aSPatrick Venture     std::vector<std::tuple<std::string, std::string, std::string>>;
33a41d282aSPatrick Williams using Attributes = std::variant<bool, AssociationList>;
34aebfde81SDhruvaraj Subhashchandran using PropertyName = std::string;
35b2f253b7SMatt Spinler using PropertyMap = std::map<PropertyName, Attributes>;
36b2f253b7SMatt Spinler using InterfaceName = std::string;
37b2f253b7SMatt Spinler using InterfaceMap = std::map<InterfaceName, PropertyMap>;
38aebfde81SDhruvaraj Subhashchandran 
39891c4769SDhruvaraj Subhashchandran using Service = std::string;
40891c4769SDhruvaraj Subhashchandran using Path = std::string;
41891c4769SDhruvaraj Subhashchandran using Interface = std::string;
42891c4769SDhruvaraj Subhashchandran using Interfaces = std::vector<Interface>;
43891c4769SDhruvaraj Subhashchandran using MapperResponseType = std::map<Path, std::map<Service, Interfaces>>;
44891c4769SDhruvaraj Subhashchandran 
453c6f29a0SDhruvaraj Subhashchandran using MethodErr =
463c6f29a0SDhruvaraj Subhashchandran     sdbusplus::xyz::openbmc_project::Led::Mapper::Error::MethodError;
473c6f29a0SDhruvaraj Subhashchandran using ObjectNotFoundErr =
483c6f29a0SDhruvaraj Subhashchandran     sdbusplus::xyz::openbmc_project::Led::Mapper::Error::ObjectNotFoundError;
4991ac8d3aSPatrick Venture using InventoryPathErr = sdbusplus::xyz::openbmc_project::Led::Fru::Monitor::
5091ac8d3aSPatrick Venture     Error::InventoryPathError;
513c6f29a0SDhruvaraj Subhashchandran 
5291ac8d3aSPatrick Venture std::string getService(sdbusplus::bus::bus& bus, const std::string& path)
533c6f29a0SDhruvaraj Subhashchandran {
5491ac8d3aSPatrick Venture     auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH,
553c6f29a0SDhruvaraj Subhashchandran                                       MAPPER_IFACE, "GetObject");
563c6f29a0SDhruvaraj Subhashchandran     mapper.append(path.c_str(), std::vector<std::string>({OBJMGR_IFACE}));
573c6f29a0SDhruvaraj Subhashchandran 
583c6f29a0SDhruvaraj Subhashchandran     std::map<std::string, std::vector<std::string>> mapperResponse;
59151122aaSWilliam A. Kennington III     try
60151122aaSWilliam A. Kennington III     {
61*d78de14dSGeorge Liu         auto mapperResponseMsg = bus.call(mapper);
623c6f29a0SDhruvaraj Subhashchandran         mapperResponseMsg.read(mapperResponse);
63151122aaSWilliam A. Kennington III     }
647152edcfSPatrick Williams     catch (const sdbusplus::exception::exception& e)
65151122aaSWilliam A. Kennington III     {
6691ac8d3aSPatrick Venture         log<level::ERR>(
67*d78de14dSGeorge Liu             "Failed to GetObject or parse getService mapper response",
68*d78de14dSGeorge Liu             entry("ERROR=%s", e.what()));
69151122aaSWilliam A. Kennington III         using namespace xyz::openbmc_project::Led::Mapper;
7091ac8d3aSPatrick Venture         elog<ObjectNotFoundErr>(ObjectNotFoundError::METHOD_NAME("GetObject"),
71151122aaSWilliam A. Kennington III                                 ObjectNotFoundError::PATH(path.c_str()),
7291ac8d3aSPatrick Venture                                 ObjectNotFoundError::INTERFACE(OBJMGR_IFACE));
73151122aaSWilliam A. Kennington III     }
743c6f29a0SDhruvaraj Subhashchandran     if (mapperResponse.empty())
753c6f29a0SDhruvaraj Subhashchandran     {
763c6f29a0SDhruvaraj Subhashchandran         using namespace xyz::openbmc_project::Led::Mapper;
7791ac8d3aSPatrick Venture         elog<ObjectNotFoundErr>(ObjectNotFoundError::METHOD_NAME("GetObject"),
783c6f29a0SDhruvaraj Subhashchandran                                 ObjectNotFoundError::PATH(path.c_str()),
7991ac8d3aSPatrick Venture                                 ObjectNotFoundError::INTERFACE(OBJMGR_IFACE));
803c6f29a0SDhruvaraj Subhashchandran     }
813c6f29a0SDhruvaraj Subhashchandran 
823c6f29a0SDhruvaraj Subhashchandran     return mapperResponse.cbegin()->first;
833c6f29a0SDhruvaraj Subhashchandran }
843c6f29a0SDhruvaraj Subhashchandran 
8591ac8d3aSPatrick Venture void action(sdbusplus::bus::bus& bus, const std::string& path, bool assert)
8659b86cd7SDhruvaraj Subhashchandran {
873c6f29a0SDhruvaraj Subhashchandran     std::string service;
883c6f29a0SDhruvaraj Subhashchandran     try
893c6f29a0SDhruvaraj Subhashchandran     {
90e77b8345SMatt Spinler         std::string groups{LED_GROUPS};
91e77b8345SMatt Spinler         groups.pop_back();
92e77b8345SMatt Spinler         service = getService(bus, groups);
933c6f29a0SDhruvaraj Subhashchandran     }
943c6f29a0SDhruvaraj Subhashchandran     catch (MethodErr& e)
953c6f29a0SDhruvaraj Subhashchandran     {
963c6f29a0SDhruvaraj Subhashchandran         commit<MethodErr>();
973c6f29a0SDhruvaraj Subhashchandran         return;
983c6f29a0SDhruvaraj Subhashchandran     }
993c6f29a0SDhruvaraj Subhashchandran     catch (ObjectNotFoundErr& e)
1003c6f29a0SDhruvaraj Subhashchandran     {
1013c6f29a0SDhruvaraj Subhashchandran         commit<ObjectNotFoundErr>();
1023c6f29a0SDhruvaraj Subhashchandran         return;
1033c6f29a0SDhruvaraj Subhashchandran     }
1043c6f29a0SDhruvaraj Subhashchandran 
1053c6f29a0SDhruvaraj Subhashchandran     auto pos = path.rfind("/");
1063c6f29a0SDhruvaraj Subhashchandran     if (pos == std::string::npos)
1073c6f29a0SDhruvaraj Subhashchandran     {
1083c6f29a0SDhruvaraj Subhashchandran         using namespace xyz::openbmc_project::Led::Fru::Monitor;
10991ac8d3aSPatrick Venture         report<InventoryPathErr>(InventoryPathError::PATH(path.c_str()));
1103c6f29a0SDhruvaraj Subhashchandran         return;
1113c6f29a0SDhruvaraj Subhashchandran     }
1123c6f29a0SDhruvaraj Subhashchandran     auto unit = path.substr(pos + 1);
1133c6f29a0SDhruvaraj Subhashchandran 
11491ac8d3aSPatrick Venture     std::string ledPath = LED_GROUPS + unit + '_' + LED_FAULT;
1153c6f29a0SDhruvaraj Subhashchandran 
11691ac8d3aSPatrick Venture     auto method = bus.new_method_call(service.c_str(), ledPath.c_str(),
11791ac8d3aSPatrick Venture                                       "org.freedesktop.DBus.Properties", "Set");
1183c6f29a0SDhruvaraj Subhashchandran     method.append("xyz.openbmc_project.Led.Group");
1193c6f29a0SDhruvaraj Subhashchandran     method.append("Asserted");
1203c6f29a0SDhruvaraj Subhashchandran 
121a41d282aSPatrick Williams     method.append(std::variant<bool>(assert));
12208d613e7SAdriana Kobylak 
12308d613e7SAdriana Kobylak     try
12408d613e7SAdriana Kobylak     {
1253c6f29a0SDhruvaraj Subhashchandran         bus.call_noreply(method);
12608d613e7SAdriana Kobylak     }
1277152edcfSPatrick Williams     catch (const sdbusplus::exception::exception& e)
12808d613e7SAdriana Kobylak     {
12908d613e7SAdriana Kobylak         // Log an info message, system may not have all the LED Groups defined
13008d613e7SAdriana Kobylak         log<level::INFO>("Failed to Assert LED Group",
13108d613e7SAdriana Kobylak                          entry("ERROR=%s", e.what()));
13208d613e7SAdriana Kobylak     }
1333c6f29a0SDhruvaraj Subhashchandran 
13459b86cd7SDhruvaraj Subhashchandran     return;
13559b86cd7SDhruvaraj Subhashchandran }
13659b86cd7SDhruvaraj Subhashchandran 
1373eedbe44SPatrick Williams void Add::created(sdbusplus::message::message& msg)
13859b86cd7SDhruvaraj Subhashchandran {
1393eedbe44SPatrick Williams     auto bus = msg.get_bus();
1403c6f29a0SDhruvaraj Subhashchandran 
141b2f253b7SMatt Spinler     sdbusplus::message::object_path objectPath;
142b2f253b7SMatt Spinler     InterfaceMap interfaces;
143151122aaSWilliam A. Kennington III     try
144151122aaSWilliam A. Kennington III     {
145b2f253b7SMatt Spinler         msg.read(objectPath, interfaces);
146151122aaSWilliam A. Kennington III     }
1477152edcfSPatrick Williams     catch (const sdbusplus::exception::exception& e)
148151122aaSWilliam A. Kennington III     {
149151122aaSWilliam A. Kennington III         log<level::ERR>("Failed to parse created message",
150151122aaSWilliam A. Kennington III                         entry("ERROR=%s", e.what()),
151151122aaSWilliam A. Kennington III                         entry("REPLY_SIG=%s", msg.get_signature()));
152151122aaSWilliam A. Kennington III         return;
153151122aaSWilliam A. Kennington III     }
1543c6f29a0SDhruvaraj Subhashchandran 
155b2f253b7SMatt Spinler     std::size_t found = objectPath.str.find(ELOG_ENTRY);
1563c6f29a0SDhruvaraj Subhashchandran     if (found == std::string::npos)
1573c6f29a0SDhruvaraj Subhashchandran     {
1583c6f29a0SDhruvaraj Subhashchandran         // Not a new error entry skip
1593eedbe44SPatrick Williams         return;
1603c6f29a0SDhruvaraj Subhashchandran     }
161a6e48929SAndrew Geissler     auto iter = interfaces.find("xyz.openbmc_project.Association.Definitions");
162b2f253b7SMatt Spinler     if (iter == interfaces.end())
1633c6f29a0SDhruvaraj Subhashchandran     {
1643eedbe44SPatrick Williams         return;
1653c6f29a0SDhruvaraj Subhashchandran     }
1663c6f29a0SDhruvaraj Subhashchandran 
1673d2b0d62SMatt Spinler     // Nothing else shows when a specific error log
1683d2b0d62SMatt Spinler     // has been created. Do it here.
169b2f253b7SMatt Spinler     std::string message{objectPath.str + " created"};
1703d2b0d62SMatt Spinler     log<level::INFO>(message.c_str());
1713d2b0d62SMatt Spinler 
172a6e48929SAndrew Geissler     auto attr = iter->second.find("Associations");
173aebfde81SDhruvaraj Subhashchandran     if (attr == iter->second.end())
1743c6f29a0SDhruvaraj Subhashchandran     {
1753eedbe44SPatrick Williams         return;
1763c6f29a0SDhruvaraj Subhashchandran     }
1773c6f29a0SDhruvaraj Subhashchandran 
1785ebebeffSPatrick Williams     auto& assocs = std::get<AssociationList>(attr->second);
1793c6f29a0SDhruvaraj Subhashchandran     if (assocs.empty())
1803c6f29a0SDhruvaraj Subhashchandran     {
1813c6f29a0SDhruvaraj Subhashchandran         // No associations skip
1823eedbe44SPatrick Williams         return;
1833c6f29a0SDhruvaraj Subhashchandran     }
1843c6f29a0SDhruvaraj Subhashchandran 
1853c6f29a0SDhruvaraj Subhashchandran     for (const auto& item : assocs)
1863c6f29a0SDhruvaraj Subhashchandran     {
1873c6f29a0SDhruvaraj Subhashchandran         if (std::get<1>(item).compare(CALLOUT_REV_ASSOCIATION) == 0)
1883c6f29a0SDhruvaraj Subhashchandran         {
1893eedbe44SPatrick Williams             removeWatches.emplace_back(
1903c6f29a0SDhruvaraj Subhashchandran                 std::make_unique<Remove>(bus, std::get<2>(item)));
191891c4769SDhruvaraj Subhashchandran             action(bus, std::get<2>(item), true);
1923c6f29a0SDhruvaraj Subhashchandran         }
1933c6f29a0SDhruvaraj Subhashchandran     }
194aebfde81SDhruvaraj Subhashchandran 
1953eedbe44SPatrick Williams     return;
19659b86cd7SDhruvaraj Subhashchandran }
19759b86cd7SDhruvaraj Subhashchandran 
19891122927SMatt Spinler void getLoggingSubTree(sdbusplus::bus::bus& bus, MapperResponseType& subtree)
199891c4769SDhruvaraj Subhashchandran {
200891c4769SDhruvaraj Subhashchandran     auto depth = 0;
20191ac8d3aSPatrick Venture     auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH,
20291ac8d3aSPatrick Venture                                           MAPPER_IFACE, "GetSubTree");
203891c4769SDhruvaraj Subhashchandran     mapperCall.append("/");
204891c4769SDhruvaraj Subhashchandran     mapperCall.append(depth);
205891c4769SDhruvaraj Subhashchandran     mapperCall.append(std::vector<Interface>({LOG_IFACE}));
206891c4769SDhruvaraj Subhashchandran 
20791122927SMatt Spinler     try
20891122927SMatt Spinler     {
209891c4769SDhruvaraj Subhashchandran         auto mapperResponseMsg = bus.call(mapperCall);
21091122927SMatt Spinler         mapperResponseMsg.read(subtree);
211151122aaSWilliam A. Kennington III     }
2127152edcfSPatrick Williams     catch (const sdbusplus::exception::exception& e)
213151122aaSWilliam A. Kennington III     {
214*d78de14dSGeorge Liu         log<level::ERR>("Failed to parse existing callouts subtree message",
215*d78de14dSGeorge Liu                         entry("ERROR=%s", e.what()));
21691122927SMatt Spinler     }
21791122927SMatt Spinler }
21891122927SMatt Spinler 
21991122927SMatt Spinler void Add::processExistingCallouts(sdbusplus::bus::bus& bus)
22091122927SMatt Spinler {
22191122927SMatt Spinler     MapperResponseType mapperResponse;
22291122927SMatt Spinler 
22391122927SMatt Spinler     getLoggingSubTree(bus, mapperResponse);
224891c4769SDhruvaraj Subhashchandran     if (mapperResponse.empty())
225891c4769SDhruvaraj Subhashchandran     {
226fc30e0c1SDhruvaraj Subhashchandran         // No errors to process.
227891c4769SDhruvaraj Subhashchandran         return;
228891c4769SDhruvaraj Subhashchandran     }
229891c4769SDhruvaraj Subhashchandran 
230891c4769SDhruvaraj Subhashchandran     for (const auto& elem : mapperResponse)
231891c4769SDhruvaraj Subhashchandran     {
23291ac8d3aSPatrick Venture         auto method = bus.new_method_call(
23391ac8d3aSPatrick Venture             elem.second.begin()->first.c_str(), elem.first.c_str(),
23491ac8d3aSPatrick Venture             "org.freedesktop.DBus.Properties", "Get");
235a6e48929SAndrew Geissler         method.append("xyz.openbmc_project.Association.Definitions");
236a6e48929SAndrew Geissler         method.append("Associations");
237891c4769SDhruvaraj Subhashchandran         auto reply = bus.call(method);
238891c4769SDhruvaraj Subhashchandran         if (reply.is_method_error())
239891c4769SDhruvaraj Subhashchandran         {
240891c4769SDhruvaraj Subhashchandran             // do not stop, continue with next elog
241891c4769SDhruvaraj Subhashchandran             log<level::ERR>("Error in getting associations");
242891c4769SDhruvaraj Subhashchandran             continue;
243891c4769SDhruvaraj Subhashchandran         }
244891c4769SDhruvaraj Subhashchandran 
245a41d282aSPatrick Williams         std::variant<AssociationList> assoc;
246151122aaSWilliam A. Kennington III         try
247151122aaSWilliam A. Kennington III         {
248891c4769SDhruvaraj Subhashchandran             reply.read(assoc);
249151122aaSWilliam A. Kennington III         }
2507152edcfSPatrick Williams         catch (const sdbusplus::exception::exception& e)
251151122aaSWilliam A. Kennington III         {
252*d78de14dSGeorge Liu             log<level::ERR>("Failed to get Associations or  parse existing "
253*d78de14dSGeorge Liu                             "callouts associations message",
254*d78de14dSGeorge Liu                             entry("ERROR=%s", e.what()));
255151122aaSWilliam A. Kennington III             continue;
256151122aaSWilliam A. Kennington III         }
2575ebebeffSPatrick Williams         auto& assocs = std::get<AssociationList>(assoc);
258891c4769SDhruvaraj Subhashchandran         if (assocs.empty())
259891c4769SDhruvaraj Subhashchandran         {
260891c4769SDhruvaraj Subhashchandran             // no associations, skip
261891c4769SDhruvaraj Subhashchandran             continue;
262891c4769SDhruvaraj Subhashchandran         }
263891c4769SDhruvaraj Subhashchandran 
264891c4769SDhruvaraj Subhashchandran         for (const auto& item : assocs)
265891c4769SDhruvaraj Subhashchandran         {
266891c4769SDhruvaraj Subhashchandran             if (std::get<1>(item).compare(CALLOUT_REV_ASSOCIATION) == 0)
267891c4769SDhruvaraj Subhashchandran             {
268891c4769SDhruvaraj Subhashchandran                 removeWatches.emplace_back(
269891c4769SDhruvaraj Subhashchandran                     std::make_unique<Remove>(bus, std::get<2>(item)));
270891c4769SDhruvaraj Subhashchandran                 action(bus, std::get<2>(item), true);
271891c4769SDhruvaraj Subhashchandran             }
272891c4769SDhruvaraj Subhashchandran         }
273891c4769SDhruvaraj Subhashchandran     }
274891c4769SDhruvaraj Subhashchandran }
275891c4769SDhruvaraj Subhashchandran 
2763eedbe44SPatrick Williams void Remove::removed(sdbusplus::message::message& msg)
27759b86cd7SDhruvaraj Subhashchandran {
2783eedbe44SPatrick Williams     auto bus = msg.get_bus();
2793c6f29a0SDhruvaraj Subhashchandran 
2803eedbe44SPatrick Williams     action(bus, inventoryPath, false);
2813eedbe44SPatrick Williams     return;
28259b86cd7SDhruvaraj Subhashchandran }
28359b86cd7SDhruvaraj Subhashchandran 
28459b86cd7SDhruvaraj Subhashchandran } // namespace monitor
28559b86cd7SDhruvaraj Subhashchandran } // namespace fault
28659b86cd7SDhruvaraj Subhashchandran } // namespace fru
28759b86cd7SDhruvaraj Subhashchandran } // namespace led
28859b86cd7SDhruvaraj Subhashchandran } // namespace phosphor
289