Matt Spinler | e812239 | 2020-09-24 13:22:18 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "fan.hpp" |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame^] | 4 | #include "power_state.hpp" |
Matt Spinler | e812239 | 2020-09-24 13:22:18 -0500 | [diff] [blame] | 5 | |
| 6 | #include <nlohmann/json.hpp> |
| 7 | #include <sdbusplus/bus.hpp> |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame^] | 8 | #include <sdbusplus/bus/match.hpp> |
| 9 | #include <sdeventplus/clock.hpp> |
| 10 | #include <sdeventplus/utility/timer.hpp> |
Matt Spinler | e812239 | 2020-09-24 13:22:18 -0500 | [diff] [blame] | 11 | |
| 12 | namespace phosphor::fan::presence |
| 13 | { |
| 14 | class 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 | */ |
| 28 | class 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 |
| 42 | * @param[in] jsonConf - The 'reporting' section of the JSON config file |
| 43 | * @param[in] fans - The fans for this configuration |
| 44 | */ |
| 45 | ErrorReporter( |
| 46 | sdbusplus::bus::bus& bus, const nlohmann::json& jsonConf, |
| 47 | const std::vector< |
| 48 | std::tuple<Fan, std::vector<std::unique_ptr<PresenceSensor>>>>& |
| 49 | fans); |
| 50 | |
| 51 | private: |
| 52 | /** |
| 53 | * @brief Reads in the configuration from the JSON section |
| 54 | * |
| 55 | * @param[in] jsonConf - The 'reporting' section of the JSON |
| 56 | */ |
| 57 | void loadConfig(const nlohmann::json& jsonConf); |
| 58 | |
| 59 | /** |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame^] | 60 | * @brief The propertiesChanged callback for the interface that |
| 61 | * contains the Present property of a fan. |
| 62 | * |
| 63 | * Will start the timer to create an event log if power is on. |
| 64 | * |
| 65 | * @param[in] msg - The payload of the propertiesChanged signal |
| 66 | */ |
| 67 | void presenceChanged(sdbusplus::message::message& msg); |
| 68 | |
| 69 | /** |
| 70 | * @brief The callback function called by the PowerState class |
| 71 | * when the power state changes. |
| 72 | * |
| 73 | * Will start timers for missing fans if power is on, and stop |
| 74 | * them when power changes to off. |
| 75 | * |
| 76 | * @param[in] powerState - The new power state |
| 77 | */ |
| 78 | void powerStateChanged(bool powerState); |
| 79 | |
| 80 | /** |
| 81 | * @brief Called when the fan missing timer expires to create |
| 82 | * an event log for a missing fan. |
| 83 | * |
| 84 | * @param[in] fanPath - The D-Bus path of the missing fan. |
| 85 | */ |
| 86 | void fanMissingTimerExpired(const std::string& fanPath); |
| 87 | |
| 88 | /** |
| 89 | * @brief Checks if the fan missing timer for a fan needs to |
| 90 | * either be started or stopped based on the power and |
| 91 | * presence states. |
| 92 | * |
| 93 | * @param[in] fanPath - The D-Bus path of the fan |
| 94 | */ |
| 95 | void checkFan(const std::string& fanPath); |
| 96 | |
| 97 | /** |
Matt Spinler | e812239 | 2020-09-24 13:22:18 -0500 | [diff] [blame] | 98 | * @brief Reference to the D-Bus connection object. |
| 99 | */ |
| 100 | sdbusplus::bus::bus& _bus; |
| 101 | |
| 102 | /** |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame^] | 103 | * @brief The connection to the event loop for the timer. |
| 104 | */ |
| 105 | sdeventplus::Event _event; |
| 106 | |
| 107 | /** |
| 108 | * @brief The propertiesChanged match objects. |
| 109 | */ |
| 110 | std::vector<sdbusplus::bus::match::match> _matches; |
| 111 | |
| 112 | /** |
| 113 | * @brief Base class pointer to the power state implementation. |
| 114 | */ |
| 115 | std::unique_ptr<PowerState> _powerState; |
| 116 | |
| 117 | /** |
Matt Spinler | e812239 | 2020-09-24 13:22:18 -0500 | [diff] [blame] | 118 | * @brief The amount of time in seconds that a fan must be missing |
| 119 | * before an event log is created for it. |
| 120 | */ |
| 121 | std::chrono::seconds _fanMissingErrorTime; |
Matt Spinler | 0dc85ee | 2020-09-24 14:07:02 -0500 | [diff] [blame^] | 122 | |
| 123 | /** |
| 124 | * @brief The map of fan paths to their presence states. |
| 125 | */ |
| 126 | std::map<std::string, bool> _fanStates; |
| 127 | |
| 128 | /** |
| 129 | * @brief The map of fan paths to their Timer objects. |
| 130 | */ |
| 131 | std::map<std::string, std::unique_ptr<sdeventplus::utility::Timer< |
| 132 | sdeventplus::ClockId::Monotonic>>> |
| 133 | _fanMissingTimers; |
Matt Spinler | e812239 | 2020-09-24 13:22:18 -0500 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | } // namespace phosphor::fan::presence |