xref: /openbmc/phosphor-fan-presence/presence/error_reporter.hpp (revision cb356d48f0ab00c4f5f774c68760d2c2afd14cd8)
1e8122390SMatt Spinler #pragma once
2e8122390SMatt Spinler 
3e8122390SMatt Spinler #include "fan.hpp"
40dc85eefSMatt Spinler #include "power_state.hpp"
5e8122390SMatt Spinler 
6e8122390SMatt Spinler #include <nlohmann/json.hpp>
7e8122390SMatt Spinler #include <sdbusplus/bus.hpp>
80dc85eefSMatt Spinler #include <sdbusplus/bus/match.hpp>
90dc85eefSMatt Spinler #include <sdeventplus/clock.hpp>
100dc85eefSMatt Spinler #include <sdeventplus/utility/timer.hpp>
11e8122390SMatt Spinler 
12e8122390SMatt Spinler namespace phosphor::fan::presence
13e8122390SMatt Spinler {
14e8122390SMatt Spinler class PresenceSensor;
15e8122390SMatt Spinler 
16e8122390SMatt Spinler /**
17e8122390SMatt Spinler  * @class ErrorReporter
18e8122390SMatt Spinler  *
19e8122390SMatt Spinler  * This class will create event logs for missing fans.  The logs are
20e8122390SMatt Spinler  * created after a fan has been missing for an amount of time
21e8122390SMatt Spinler  * specified in the JSON config file while power is on.
22e8122390SMatt Spinler  *
23e8122390SMatt Spinler  * The timers to create event logs are not started when power is off.
24e8122390SMatt Spinler  * When power is turned on, the timers for any missing fans will be
25e8122390SMatt Spinler  * be started.  If any timers are running when power is turned off,
26e8122390SMatt Spinler  * they will be stopped.
27e8122390SMatt Spinler  */
28e8122390SMatt Spinler class ErrorReporter
29e8122390SMatt Spinler {
30e8122390SMatt Spinler   public:
31e8122390SMatt Spinler     ErrorReporter() = delete;
32e8122390SMatt Spinler     ~ErrorReporter() = default;
33e8122390SMatt Spinler     ErrorReporter(const ErrorReporter&) = delete;
34e8122390SMatt Spinler     ErrorReporter& operator=(const ErrorReporter&) = delete;
35e8122390SMatt Spinler     ErrorReporter(ErrorReporter&&) = delete;
36e8122390SMatt Spinler     ErrorReporter& operator=(ErrorReporter&&) = delete;
37e8122390SMatt Spinler 
38e8122390SMatt Spinler     /**
39e8122390SMatt Spinler      * @brief Constructor
40e8122390SMatt Spinler      *
41e8122390SMatt Spinler      * @param[in] bus - The sdbusplus bus object
42e8122390SMatt Spinler      * @param[in] fans - The fans for this configuration
43e8122390SMatt Spinler      */
44e8122390SMatt Spinler     ErrorReporter(
45*cb356d48SPatrick Williams         sdbusplus::bus_t& bus,
46e8122390SMatt Spinler         const std::vector<
47e8122390SMatt Spinler             std::tuple<Fan, std::vector<std::unique_ptr<PresenceSensor>>>>&
48e8122390SMatt Spinler             fans);
49e8122390SMatt Spinler 
50e8122390SMatt Spinler   private:
51e8122390SMatt Spinler     /**
520dc85eefSMatt Spinler      * @brief The propertiesChanged callback for the interface that
530dc85eefSMatt Spinler      *        contains the Present property of a fan.
540dc85eefSMatt Spinler      *
550dc85eefSMatt Spinler      * Will start the timer to create an event log if power is on.
560dc85eefSMatt Spinler      *
570dc85eefSMatt Spinler      * @param[in] msg - The payload of the propertiesChanged signal
580dc85eefSMatt Spinler      */
59*cb356d48SPatrick Williams     void presenceChanged(sdbusplus::message_t& msg);
600dc85eefSMatt Spinler 
610dc85eefSMatt Spinler     /**
620dc85eefSMatt Spinler      * @brief The callback function called by the PowerState class
630dc85eefSMatt Spinler      *        when the power state changes.
640dc85eefSMatt Spinler      *
650dc85eefSMatt Spinler      * Will start timers for missing fans if power is on, and stop
660dc85eefSMatt Spinler      * them when power changes to off.
670dc85eefSMatt Spinler      *
680dc85eefSMatt Spinler      * @param[in] powerState - The new power state
690dc85eefSMatt Spinler      */
700dc85eefSMatt Spinler     void powerStateChanged(bool powerState);
710dc85eefSMatt Spinler 
720dc85eefSMatt Spinler     /**
730dc85eefSMatt Spinler      * @brief Called when the fan missing timer expires to create
740dc85eefSMatt Spinler      *        an event log for a missing fan.
750dc85eefSMatt Spinler      *
760dc85eefSMatt Spinler      * @param[in] fanPath - The D-Bus path of the missing fan.
770dc85eefSMatt Spinler      */
780dc85eefSMatt Spinler     void fanMissingTimerExpired(const std::string& fanPath);
790dc85eefSMatt Spinler 
800dc85eefSMatt Spinler     /**
810dc85eefSMatt Spinler      * @brief Checks if the fan missing timer for a fan needs to
820dc85eefSMatt Spinler      *        either be started or stopped based on the power and
830dc85eefSMatt Spinler      *        presence states.
840dc85eefSMatt Spinler      *
850dc85eefSMatt Spinler      * @param[in] fanPath - The D-Bus path of the fan
860dc85eefSMatt Spinler      */
870dc85eefSMatt Spinler     void checkFan(const std::string& fanPath);
880dc85eefSMatt Spinler 
890dc85eefSMatt Spinler     /**
90e8122390SMatt Spinler      * @brief Reference to the D-Bus connection object.
91e8122390SMatt Spinler      */
92*cb356d48SPatrick Williams     sdbusplus::bus_t& _bus;
93e8122390SMatt Spinler 
94e8122390SMatt Spinler     /**
950dc85eefSMatt Spinler      * @brief The connection to the event loop for the timer.
960dc85eefSMatt Spinler      */
970dc85eefSMatt Spinler     sdeventplus::Event _event;
980dc85eefSMatt Spinler 
990dc85eefSMatt Spinler     /**
1000dc85eefSMatt Spinler      * @brief The propertiesChanged match objects.
1010dc85eefSMatt Spinler      */
102*cb356d48SPatrick Williams     std::vector<sdbusplus::bus::match_t> _matches;
1030dc85eefSMatt Spinler 
1040dc85eefSMatt Spinler     /**
1050dc85eefSMatt Spinler      * @brief Base class pointer to the power state implementation.
1060dc85eefSMatt Spinler      */
10776e73c2aSMatt Spinler     std::shared_ptr<PowerState> _powerState;
1080dc85eefSMatt Spinler 
1090dc85eefSMatt Spinler     /**
1100dc85eefSMatt Spinler      * @brief The map of fan paths to their presence states.
1110dc85eefSMatt Spinler      */
1120dc85eefSMatt Spinler     std::map<std::string, bool> _fanStates;
1130dc85eefSMatt Spinler 
1140dc85eefSMatt Spinler     /**
1159e9f599cSMatt Spinler      * @brief The map of fan paths to their Timer objects with
1169e9f599cSMatt Spinler      *        the timer expiration time.
1170dc85eefSMatt Spinler      */
1189e9f599cSMatt Spinler     std::map<std::string,
1199e9f599cSMatt Spinler              std::tuple<std::unique_ptr<sdeventplus::utility::Timer<
1209e9f599cSMatt Spinler                             sdeventplus::ClockId::Monotonic>>,
1219e9f599cSMatt Spinler                         std::chrono::seconds>>
1220dc85eefSMatt Spinler         _fanMissingTimers;
123e8122390SMatt Spinler };
124e8122390SMatt Spinler 
125e8122390SMatt Spinler } // namespace phosphor::fan::presence
126