blob: a0030dae9f611d1e8c62a21a979d55f6099e2ad5 [file] [log] [blame]
Matthew Barthc95c5272020-06-15 19:51:13 -05001/**
2 * Copyright © 2020 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#pragma once
17
18#include "fan.hpp"
Matt Spinlerac1efc12020-10-27 10:20:11 -050019#include "fan_error.hpp"
Matt Spinlere892e392020-10-14 13:21:31 -050020#include "power_off_rule.hpp"
21#include "power_state.hpp"
Matthew Barthc95c5272020-06-15 19:51:13 -050022#include "tach_sensor.hpp"
23#include "trust_manager.hpp"
24#include "types.hpp"
25
26#include <nlohmann/json.hpp>
27#include <sdbusplus/bus.hpp>
28#include <sdeventplus/event.hpp>
Matthew Barthd06905c2020-06-12 08:13:06 -050029#include <sdeventplus/source/signal.hpp>
Matthew Barthc95c5272020-06-15 19:51:13 -050030
31#include <memory>
32#include <optional>
33#include <vector>
34
35namespace phosphor::fan::monitor
36{
37
38using json = nlohmann::json;
39
40class System
41{
42 public:
43 System() = delete;
Matt Spinlerac1efc12020-10-27 10:20:11 -050044 ~System() = default;
Matthew Barthc95c5272020-06-15 19:51:13 -050045 System(const System&) = delete;
46 System(System&&) = delete;
47 System& operator=(const System&) = delete;
48 System& operator=(System&&) = delete;
Matthew Barthc95c5272020-06-15 19:51:13 -050049
50 /**
51 * Constructor
52 * Parses and populates the fan monitor trust groups and list of fans
53 *
54 * @param[in] mode - mode of fan monitor
55 * @param[in] bus - sdbusplus bus object
56 * @param[in] event - event loop reference
57 */
58 System(Mode mode, sdbusplus::bus::bus& bus,
59 const sdeventplus::Event& event);
60
Matthew Barthd06905c2020-06-12 08:13:06 -050061 /**
62 * @brief Callback function to handle receiving a HUP signal to reload the
63 * JSON configuration.
64 */
65 void sighupHandler(sdeventplus::source::Signal&,
66 const struct signalfd_siginfo*);
67
Matt Spinlerb63aa092020-10-14 09:45:11 -050068 /**
69 * @brief Called from the fan when it changes either
70 * present or functional status to update the
71 * fan health map.
72 *
73 * @param[in] fan - The fan that changed
74 */
75 void fanStatusChange(const Fan& fan);
76
Matt Spinlerf13b42e2020-10-26 15:29:49 -050077 /**
78 * @brief Called when a fan sensor's error timer expires, which
79 * happens when the sensor has been nonfunctional for a
80 * certain amount of time. An event log will be created.
81 *
82 * @param[in] fan - The parent fan of the sensor
83 * @param[in] sensor - The faulted sensor
84 */
85 void sensorErrorTimerExpired(const Fan& fan, const TachSensor& sensor);
86
Matt Spinler27f6b682020-10-27 08:43:37 -050087 /**
88 * @brief Called when the timer that starts when a fan is missing
89 * has expired so an event log needs to be created.
90 *
91 * @param[in] fan - The missing fan.
92 */
93 void fanMissingErrorTimerExpired(const Fan& fan);
94
Matt Spinlerac1efc12020-10-27 10:20:11 -050095 /**
96 * @brief Called by the power off actions to log an error when there is
97 * a power off due to fan problems.
98 *
99 * The error it logs is just the last fan error that occurred.
100 */
101 void logShutdownError();
102
Matthew Barthc95c5272020-06-15 19:51:13 -0500103 private:
104 /* The mode of fan monitor */
105 Mode _mode;
106
107 /* The sdbusplus bus object */
108 sdbusplus::bus::bus& _bus;
109
110 /* The event loop reference */
111 const sdeventplus::Event& _event;
112
113 /* Trust manager of trust groups */
114 std::unique_ptr<phosphor::fan::trust::Manager> _trust;
115
116 /* List of fan objects to monitor */
117 std::vector<std::unique_ptr<Fan>> _fans;
118
119 /**
Matt Spinlerb63aa092020-10-14 09:45:11 -0500120 * @brief The latest health of all the fans
121 */
122 FanHealth _fanHealth;
123
124 /**
Matt Spinlere892e392020-10-14 13:21:31 -0500125 * @brief The object to watch the power state
126 */
127 std::unique_ptr<PowerState> _powerState;
128
129 /**
130 * @brief The power off rules, for shutting down the system
131 * due to fan failures.
132 */
133 std::vector<std::unique_ptr<PowerOffRule>> _powerOffRules;
134
135 /**
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500136 * @brief The number of concurrently nonfunctional fan sensors
137 * there must be for an event log created due to a
138 * nonfunctional fan sensor to have an Error severity as
139 * opposed to an Informational one.
140 */
141 std::optional<size_t> _numNonfuncSensorsBeforeError;
142
143 /**
Matt Spinlerac1efc12020-10-27 10:20:11 -0500144 * @brief The most recently committed fan error.
145 */
146 std::unique_ptr<FanError> _lastError;
147
148 /**
Matt Spinlerc8d3c512021-01-06 14:22:25 -0600149 * @brief The thermal alert D-Bus object
150 */
151 ThermalAlertObject _thermalAlert;
152
153 /**
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500154 * @brief Captures tach sensor data as JSON for use in
155 * fan fault and fan missing event logs.
156 *
157 * @return json - The JSON data
158 */
159 json captureSensorData();
160
161 /**
Matthew Barthc95c5272020-06-15 19:51:13 -0500162 * @brief Retrieve the configured trust groups
163 *
164 * @param[in] jsonObj - JSON object to parse from
165 *
166 * @return List of functions applied on trust groups
167 */
168 const std::vector<CreateGroupFunction> getTrustGroups(const json& jsonObj);
169
170 /**
Matthew Barthd06905c2020-06-12 08:13:06 -0500171 * @brief Set the trust manager's list of trust group functions
172 *
173 * @param[in] groupFuncs - list of trust group functions
174 */
175 void setTrustMgr(const std::vector<CreateGroupFunction>& groupFuncs);
176
177 /**
Matthew Barthc95c5272020-06-15 19:51:13 -0500178 * @brief Retrieve the configured fan definitions
179 *
180 * @param[in] jsonObj - JSON object to parse from
181 *
182 * @return List of fan definition data on the fans configured
183 */
184 const std::vector<FanDefinition> getFanDefinitions(const json& jsonObj);
Matthew Barthd06905c2020-06-12 08:13:06 -0500185
186 /**
187 * @brief Set the list of fans to be monitored
188 *
189 * @param[in] fanDefs - list of fan definitions to create fans monitored
190 */
191 void setFans(const std::vector<FanDefinition>& fanDefs);
Matt Spinlerb63aa092020-10-14 09:45:11 -0500192
193 /**
194 * @brief Updates the fan health map entry for the fan passed in
195 *
196 * @param[in] fan - The fan to update the health map with
197 */
198 void updateFanHealth(const Fan& fan);
Matt Spinlere892e392020-10-14 13:21:31 -0500199
200 /**
201 * @brief The function that runs when the power state changes
202 *
203 * @param[in] powerStateOn - If power is now on or not
204 */
205 void powerStateChanged(bool powerStateOn);
206
207 /**
208 * @brief Reads the fault configuration from the JSON config
209 * file, such as the power off rule configuration.
210 *
211 * @param[in] jsonObj - JSON object to parse from
212 */
213 void setFaultConfig(const json& jsonObj);
Matthew Barthc95c5272020-06-15 19:51:13 -0500214};
215
216} // namespace phosphor::fan::monitor