1 /**
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"
19 #include "fan_error.hpp"
20 #include "power_off_rule.hpp"
21 #include "power_state.hpp"
22 #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>
29 #include <sdeventplus/source/signal.hpp>
30 
31 #include <memory>
32 #include <optional>
33 #include <vector>
34 
35 namespace phosphor::fan::monitor
36 {
37 
38 using json = nlohmann::json;
39 
40 // Mapping from service name to sensor
41 using SensorMapType =
42     std::map<std::string, std::set<std::shared_ptr<TachSensor>>>;
43 
44 class System
45 {
46   public:
47     System() = delete;
48     ~System() = default;
49     System(const System&) = delete;
50     System(System&&) = delete;
51     System& operator=(const System&) = delete;
52     System& operator=(System&&) = delete;
53 
54     /**
55      * Constructor
56      *
57      * @param[in] mode - mode of fan monitor
58      * @param[in] bus - sdbusplus bus object
59      * @param[in] event - event loop reference
60      */
61     System(Mode mode, sdbusplus::bus::bus& bus,
62            const sdeventplus::Event& event);
63 
64     /**
65      * @brief Callback function to handle receiving a HUP signal to reload the
66      * JSON configuration.
67      */
68     void sighupHandler(sdeventplus::source::Signal&,
69                        const struct signalfd_siginfo*);
70 
71     /**
72      * @brief Called from the fan when it changes either
73      *        present or functional status to update the
74      *        fan health map.
75      *
76      * @param[in] fan - The fan that changed
77      * @param[in] skipRulesCheck - If the rules checks should be done now.
78      */
79     void fanStatusChange(const Fan& fan, bool skipRulesCheck = false);
80 
81     /**
82      * @brief Called when a fan sensor's error timer expires, which
83      *        happens when the sensor has been nonfunctional for a
84      *        certain amount of time.  An event log will be created.
85      *
86      * @param[in] fan - The parent fan of the sensor
87      * @param[in] sensor - The faulted sensor
88      */
89     void sensorErrorTimerExpired(const Fan& fan, const TachSensor& sensor);
90 
91     /**
92      * @brief Called when the timer that starts when a fan is missing
93      *        has expired so an event log needs to be created.
94      *
95      * @param[in] fan - The missing fan.
96      */
97     void fanMissingErrorTimerExpired(const Fan& fan);
98 
99     /**
100      * @brief Called by the power off actions to log an error when there is
101      *        a power off due to fan problems.
102      *
103      * The error it logs is just the last fan error that occurred.
104      */
105     void logShutdownError();
106 
107     /**
108      * @brief Returns true if power is on
109      */
110     bool isPowerOn() const
111     {
112         return _powerState->isPowerOn();
113     }
114 
115     /**
116      * @brief tests the presence of Inventory and calls load() if present, else
117      *  waits for Inventory asynchronously and has a callback to load() when
118      * present
119      */
120     void start();
121 
122     /**
123      * @brief Parses and populates the fan monitor trust groups and list of fans
124      */
125     void load();
126 
127   private:
128     /**
129      * @brief Callback from D-Bus when Inventory service comes online
130      *
131      * @param[in] msg - Service details.
132      */
133     void inventoryOnlineCb(sdbusplus::message::message& msg);
134 
135     /* The mode of fan monitor */
136     Mode _mode;
137 
138     /* The sdbusplus bus object */
139     sdbusplus::bus::bus& _bus;
140 
141     /* The event loop reference */
142     const sdeventplus::Event& _event;
143 
144     /* Trust manager of trust groups */
145     std::unique_ptr<phosphor::fan::trust::Manager> _trust;
146 
147     /* match object to detect Inventory service */
148     std::unique_ptr<sdbusplus::bus::match::match> _inventoryMatch;
149 
150     /* List of fan objects to monitor */
151     std::vector<std::unique_ptr<Fan>> _fans;
152 
153     /**
154      * @brief The latest health of all the fans
155      */
156     FanHealth _fanHealth;
157 
158     /**
159      * @brief The object to watch the power state
160      */
161     std::unique_ptr<PowerState> _powerState;
162 
163     /**
164      * @brief The power off rules, for shutting down the system
165      *        due to fan failures.
166      */
167     std::vector<std::unique_ptr<PowerOffRule>> _powerOffRules;
168 
169     /**
170      * @brief The number of concurrently nonfunctional fan sensors
171      *        there must be for an event log created due to a
172      *        nonfunctional fan sensor to have an Error severity as
173      *        opposed to an Informational one.
174      */
175     std::optional<size_t> _numNonfuncSensorsBeforeError;
176 
177     /**
178      * @brief The most recently committed fan error.
179      */
180     std::unique_ptr<FanError> _lastError;
181 
182     /**
183      * @brief The thermal alert D-Bus object
184      */
185     ThermalAlertObject _thermalAlert;
186 
187     /**
188      * @brief The tach sensors D-Bus match objects
189      */
190     std::vector<std::unique_ptr<sdbusplus::bus::match::match>> _sensorMatch;
191 
192     /**
193      * @brief true if config files have been loaded
194      */
195     bool _loaded = false;
196 
197     /**
198      * @brief Captures tach sensor data as JSON for use in
199      *        fan fault and fan missing event logs.
200      *
201      * @return json - The JSON data
202      */
203     json captureSensorData();
204 
205     /**
206      * @brief creates a subscription (service->sensor) to take sensors
207      *        on/offline when D-Bus starts/stops updating values
208      *
209      */
210     void subscribeSensorsToServices();
211 
212     /**
213      * @brief Retrieve the configured trust groups
214      *
215      * @param[in] jsonObj - JSON object to parse from
216      *
217      * @return List of functions applied on trust groups
218      */
219     const std::vector<CreateGroupFunction> getTrustGroups(const json& jsonObj);
220 
221     /**
222      * @brief Set the trust manager's list of trust group functions
223      *
224      * @param[in] groupFuncs - list of trust group functions
225      */
226     void setTrustMgr(const std::vector<CreateGroupFunction>& groupFuncs);
227 
228     /**
229      * @brief Retrieve the configured fan definitions
230      *
231      * @param[in] jsonObj - JSON object to parse from
232      *
233      * @return List of fan definition data on the fans configured
234      */
235     const std::vector<FanDefinition> getFanDefinitions(const json& jsonObj);
236 
237     /**
238      * @brief Set the list of fans to be monitored
239      *
240      * @param[in] fanDefs - list of fan definitions to create fans monitored
241      */
242     void setFans(const std::vector<FanDefinition>& fanDefs);
243 
244     /**
245      * @brief Updates the fan health map entry for the fan passed in
246      *
247      * @param[in] fan - The fan to update the health map with
248      */
249     void updateFanHealth(const Fan& fan);
250 
251     /**
252      * @brief callback when a tach sensor signal goes offline
253      *
254      * @param[in] msg - D-Bus message containing details (inc. service name)
255      *
256      * @param[in] sensorMap - map providing sensor access for each service
257      */
258     void tachSignalOffline(sdbusplus::message::message& msg,
259                            const SensorMapType& sensorMap);
260 
261     /**
262      * @brief The function that runs when the power state changes
263      *
264      * @param[in] powerStateOn - If power is now on or not
265      */
266     void powerStateChanged(bool powerStateOn);
267 
268     /**
269      * @brief Reads the fault configuration from the JSON config
270      *        file, such as the power off rule configuration.
271      *
272      * @param[in] jsonObj - JSON object to parse from
273      */
274     void setFaultConfig(const json& jsonObj);
275 
276     /**
277      * @brief Log an error and shut down due to an offline fan controller
278      */
279     void handleOfflineFanController();
280 };
281 
282 } // namespace phosphor::fan::monitor
283