blob: a6a86fefb7446858331a4f53b7682163d865dd47 [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(
Matt Spinler9e9f5992020-09-30 08:29:24 -050045 sdbusplus::bus::bus& bus,
Matt Spinlere8122392020-09-24 13:22:18 -050046 const std::vector<
47 std::tuple<Fan, std::vector<std::unique_ptr<PresenceSensor>>>>&
48 fans);
49
50 private:
51 /**
Matt Spinler0dc85ee2020-09-24 14:07:02 -050052 * @brief The propertiesChanged callback for the interface that
53 * contains the Present property of a fan.
54 *
55 * Will start the timer to create an event log if power is on.
56 *
57 * @param[in] msg - The payload of the propertiesChanged signal
58 */
59 void presenceChanged(sdbusplus::message::message& msg);
60
61 /**
62 * @brief The callback function called by the PowerState class
63 * when the power state changes.
64 *
65 * Will start timers for missing fans if power is on, and stop
66 * them when power changes to off.
67 *
68 * @param[in] powerState - The new power state
69 */
70 void powerStateChanged(bool powerState);
71
72 /**
73 * @brief Called when the fan missing timer expires to create
74 * an event log for a missing fan.
75 *
76 * @param[in] fanPath - The D-Bus path of the missing fan.
77 */
78 void fanMissingTimerExpired(const std::string& fanPath);
79
80 /**
81 * @brief Checks if the fan missing timer for a fan needs to
82 * either be started or stopped based on the power and
83 * presence states.
84 *
85 * @param[in] fanPath - The D-Bus path of the fan
86 */
87 void checkFan(const std::string& fanPath);
88
89 /**
Matt Spinlere8122392020-09-24 13:22:18 -050090 * @brief Reference to the D-Bus connection object.
91 */
92 sdbusplus::bus::bus& _bus;
93
94 /**
Matt Spinler0dc85ee2020-09-24 14:07:02 -050095 * @brief The connection to the event loop for the timer.
96 */
97 sdeventplus::Event _event;
98
99 /**
100 * @brief The propertiesChanged match objects.
101 */
102 std::vector<sdbusplus::bus::match::match> _matches;
103
104 /**
105 * @brief Base class pointer to the power state implementation.
106 */
107 std::unique_ptr<PowerState> _powerState;
108
109 /**
Matt Spinler0dc85ee2020-09-24 14:07:02 -0500110 * @brief The map of fan paths to their presence states.
111 */
112 std::map<std::string, bool> _fanStates;
113
114 /**
Matt Spinler9e9f5992020-09-30 08:29:24 -0500115 * @brief The map of fan paths to their Timer objects with
116 * the timer expiration time.
Matt Spinler0dc85ee2020-09-24 14:07:02 -0500117 */
Matt Spinler9e9f5992020-09-30 08:29:24 -0500118 std::map<std::string,
119 std::tuple<std::unique_ptr<sdeventplus::utility::Timer<
120 sdeventplus::ClockId::Monotonic>>,
121 std::chrono::seconds>>
Matt Spinler0dc85ee2020-09-24 14:07:02 -0500122 _fanMissingTimers;
Matt Spinlere8122392020-09-24 13:22:18 -0500123};
124
125} // namespace phosphor::fan::presence