blob: ef1111b20bfd5396f875fbe19cd2e0418fdf5692 [file] [log] [blame]
Matt Spinlere8122392020-09-24 13:22:18 -05001#pragma once
2
3#include "fan.hpp"
Matt Spinler0dc85ee2020-09-24 14:07:02 -05004#include "power_state.hpp"
Matt Spinlere8122392020-09-24 13:22:18 -05005
6#include <nlohmann/json.hpp>
7#include <sdbusplus/bus.hpp>
Matt Spinler0dc85ee2020-09-24 14:07:02 -05008#include <sdbusplus/bus/match.hpp>
9#include <sdeventplus/clock.hpp>
10#include <sdeventplus/utility/timer.hpp>
Matt Spinlere8122392020-09-24 13:22:18 -050011
12namespace phosphor::fan::presence
13{
14class PresenceSensor;
15
16/**
17 * @class ErrorReporter
18 *
19 * This class will create event logs for missing fans. The logs are
20 * created after a fan has been missing for an amount of time
21 * specified in the JSON config file while power is on.
22 *
23 * The timers to create event logs are not started when power is off.
24 * When power is turned on, the timers for any missing fans will be
25 * be started. If any timers are running when power is turned off,
26 * they will be stopped.
27 */
28class ErrorReporter
29{
30 public:
31 ErrorReporter() = delete;
32 ~ErrorReporter() = default;
33 ErrorReporter(const ErrorReporter&) = delete;
34 ErrorReporter& operator=(const ErrorReporter&) = delete;
35 ErrorReporter(ErrorReporter&&) = delete;
36 ErrorReporter& operator=(ErrorReporter&&) = delete;
37
38 /**
39 * @brief Constructor
40 *
41 * @param[in] bus - The sdbusplus bus object
Matt Spinlere8122392020-09-24 13:22:18 -050042 * @param[in] fans - The fans for this configuration
43 */
44 ErrorReporter(
Patrick Williamscb356d42022-07-22 19:26:53 -050045 sdbusplus::bus_t& bus,
Patrick Williamsdfddd642024-08-16 15:21:51 -040046 const std::vector<std::tuple<
47 Fan, std::vector<std::unique_ptr<PresenceSensor>>>>& fans);
Matt Spinlere8122392020-09-24 13:22:18 -050048
49 private:
50 /**
Matt Spinler0dc85ee2020-09-24 14:07:02 -050051 * @brief The propertiesChanged callback for the interface that
52 * contains the Present property of a fan.
53 *
54 * Will start the timer to create an event log if power is on.
55 *
56 * @param[in] msg - The payload of the propertiesChanged signal
57 */
Patrick Williamscb356d42022-07-22 19:26:53 -050058 void presenceChanged(sdbusplus::message_t& msg);
Matt Spinler0dc85ee2020-09-24 14:07:02 -050059
60 /**
61 * @brief The callback function called by the PowerState class
62 * when the power state changes.
63 *
64 * Will start timers for missing fans if power is on, and stop
65 * them when power changes to off.
66 *
67 * @param[in] powerState - The new power state
68 */
69 void powerStateChanged(bool powerState);
70
71 /**
72 * @brief Called when the fan missing timer expires to create
73 * an event log for a missing fan.
74 *
75 * @param[in] fanPath - The D-Bus path of the missing fan.
76 */
77 void fanMissingTimerExpired(const std::string& fanPath);
78
79 /**
80 * @brief Checks if the fan missing timer for a fan needs to
81 * either be started or stopped based on the power and
82 * presence states.
83 *
84 * @param[in] fanPath - The D-Bus path of the fan
85 */
86 void checkFan(const std::string& fanPath);
87
88 /**
Matt Spinlere8122392020-09-24 13:22:18 -050089 * @brief Reference to the D-Bus connection object.
90 */
Patrick Williamscb356d42022-07-22 19:26:53 -050091 sdbusplus::bus_t& _bus;
Matt Spinlere8122392020-09-24 13:22:18 -050092
93 /**
Matt Spinler0dc85ee2020-09-24 14:07:02 -050094 * @brief The connection to the event loop for the timer.
95 */
96 sdeventplus::Event _event;
97
98 /**
99 * @brief The propertiesChanged match objects.
100 */
Patrick Williamscb356d42022-07-22 19:26:53 -0500101 std::vector<sdbusplus::bus::match_t> _matches;
Matt Spinler0dc85ee2020-09-24 14:07:02 -0500102
103 /**
104 * @brief Base class pointer to the power state implementation.
105 */
Matt Spinler76e73c22021-04-21 11:03:05 -0500106 std::shared_ptr<PowerState> _powerState;
Matt Spinler0dc85ee2020-09-24 14:07:02 -0500107
108 /**
Matt Spinler0dc85ee2020-09-24 14:07:02 -0500109 * @brief The map of fan paths to their presence states.
110 */
111 std::map<std::string, bool> _fanStates;
112
113 /**
Matt Spinler9e9f5992020-09-30 08:29:24 -0500114 * @brief The map of fan paths to their Timer objects with
115 * the timer expiration time.
Matt Spinler0dc85ee2020-09-24 14:07:02 -0500116 */
Matt Spinler9e9f5992020-09-30 08:29:24 -0500117 std::map<std::string,
118 std::tuple<std::unique_ptr<sdeventplus::utility::Timer<
119 sdeventplus::ClockId::Monotonic>>,
120 std::chrono::seconds>>
Matt Spinler0dc85ee2020-09-24 14:07:02 -0500121 _fanMissingTimers;
Matt Spinlere8122392020-09-24 13:22:18 -0500122};
123
124} // namespace phosphor::fan::presence