xref: /openbmc/phosphor-fan-presence/presence/error_reporter.hpp (revision dfddd648cb81b27492afead4e2346f5fcd1397cb)
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(
45cb356d48SPatrick Williams         sdbusplus::bus_t& bus,
46*dfddd648SPatrick Williams         const std::vector<std::tuple<
47*dfddd648SPatrick Williams             Fan, std::vector<std::unique_ptr<PresenceSensor>>>>& fans);
48e8122390SMatt Spinler 
49e8122390SMatt Spinler   private:
50e8122390SMatt Spinler     /**
510dc85eefSMatt Spinler      * @brief The propertiesChanged callback for the interface that
520dc85eefSMatt Spinler      *        contains the Present property of a fan.
530dc85eefSMatt Spinler      *
540dc85eefSMatt Spinler      * Will start the timer to create an event log if power is on.
550dc85eefSMatt Spinler      *
560dc85eefSMatt Spinler      * @param[in] msg - The payload of the propertiesChanged signal
570dc85eefSMatt Spinler      */
58cb356d48SPatrick Williams     void presenceChanged(sdbusplus::message_t& msg);
590dc85eefSMatt Spinler 
600dc85eefSMatt Spinler     /**
610dc85eefSMatt Spinler      * @brief The callback function called by the PowerState class
620dc85eefSMatt Spinler      *        when the power state changes.
630dc85eefSMatt Spinler      *
640dc85eefSMatt Spinler      * Will start timers for missing fans if power is on, and stop
650dc85eefSMatt Spinler      * them when power changes to off.
660dc85eefSMatt Spinler      *
670dc85eefSMatt Spinler      * @param[in] powerState - The new power state
680dc85eefSMatt Spinler      */
690dc85eefSMatt Spinler     void powerStateChanged(bool powerState);
700dc85eefSMatt Spinler 
710dc85eefSMatt Spinler     /**
720dc85eefSMatt Spinler      * @brief Called when the fan missing timer expires to create
730dc85eefSMatt Spinler      *        an event log for a missing fan.
740dc85eefSMatt Spinler      *
750dc85eefSMatt Spinler      * @param[in] fanPath - The D-Bus path of the missing fan.
760dc85eefSMatt Spinler      */
770dc85eefSMatt Spinler     void fanMissingTimerExpired(const std::string& fanPath);
780dc85eefSMatt Spinler 
790dc85eefSMatt Spinler     /**
800dc85eefSMatt Spinler      * @brief Checks if the fan missing timer for a fan needs to
810dc85eefSMatt Spinler      *        either be started or stopped based on the power and
820dc85eefSMatt Spinler      *        presence states.
830dc85eefSMatt Spinler      *
840dc85eefSMatt Spinler      * @param[in] fanPath - The D-Bus path of the fan
850dc85eefSMatt Spinler      */
860dc85eefSMatt Spinler     void checkFan(const std::string& fanPath);
870dc85eefSMatt Spinler 
880dc85eefSMatt Spinler     /**
89e8122390SMatt Spinler      * @brief Reference to the D-Bus connection object.
90e8122390SMatt Spinler      */
91cb356d48SPatrick Williams     sdbusplus::bus_t& _bus;
92e8122390SMatt Spinler 
93e8122390SMatt Spinler     /**
940dc85eefSMatt Spinler      * @brief The connection to the event loop for the timer.
950dc85eefSMatt Spinler      */
960dc85eefSMatt Spinler     sdeventplus::Event _event;
970dc85eefSMatt Spinler 
980dc85eefSMatt Spinler     /**
990dc85eefSMatt Spinler      * @brief The propertiesChanged match objects.
1000dc85eefSMatt Spinler      */
101cb356d48SPatrick Williams     std::vector<sdbusplus::bus::match_t> _matches;
1020dc85eefSMatt Spinler 
1030dc85eefSMatt Spinler     /**
1040dc85eefSMatt Spinler      * @brief Base class pointer to the power state implementation.
1050dc85eefSMatt Spinler      */
10676e73c2aSMatt Spinler     std::shared_ptr<PowerState> _powerState;
1070dc85eefSMatt Spinler 
1080dc85eefSMatt Spinler     /**
1090dc85eefSMatt Spinler      * @brief The map of fan paths to their presence states.
1100dc85eefSMatt Spinler      */
1110dc85eefSMatt Spinler     std::map<std::string, bool> _fanStates;
1120dc85eefSMatt Spinler 
1130dc85eefSMatt Spinler     /**
1149e9f599cSMatt Spinler      * @brief The map of fan paths to their Timer objects with
1159e9f599cSMatt Spinler      *        the timer expiration time.
1160dc85eefSMatt Spinler      */
1179e9f599cSMatt Spinler     std::map<std::string,
1189e9f599cSMatt Spinler              std::tuple<std::unique_ptr<sdeventplus::utility::Timer<
1199e9f599cSMatt Spinler                             sdeventplus::ClockId::Monotonic>>,
1209e9f599cSMatt Spinler                         std::chrono::seconds>>
1210dc85eefSMatt Spinler         _fanMissingTimers;
122e8122390SMatt Spinler };
123e8122390SMatt Spinler 
124e8122390SMatt Spinler } // namespace phosphor::fan::presence
125