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 "compatible_system_types_finder.hpp"
19 #include "services.hpp"
20 #include "system.hpp"
21 
22 #include <interfaces/manager_interface.hpp>
23 #include <sdbusplus/bus.hpp>
24 #include <sdbusplus/server/object.hpp>
25 #include <sdeventplus/event.hpp>
26 #include <sdeventplus/source/signal.hpp>
27 #include <sdeventplus/utility/timer.hpp>
28 
29 #include <filesystem>
30 #include <memory>
31 #include <string>
32 #include <vector>
33 
34 namespace phosphor::power::regulators
35 {
36 
37 using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>;
38 
39 using ManagerObject = sdbusplus::server::object_t<
40     phosphor::power::regulators::interface::ManagerInterface>;
41 
42 class Manager : public ManagerObject
43 {
44   public:
45     Manager() = delete;
46     Manager(const Manager&) = delete;
47     Manager(Manager&&) = delete;
48     Manager& operator=(const Manager&) = delete;
49     Manager& operator=(Manager&&) = delete;
50     ~Manager() = default;
51 
52     /**
53      * Constructor
54      * Creates a manager over the regulators.
55      *
56      * @param bus the D-Bus bus
57      * @param event the sdevent event
58      */
59     Manager(sdbusplus::bus_t& bus, const sdeventplus::Event& event);
60 
61     /**
62      * Implements the D-Bus "configure" method.
63      *
64      * Configures all the voltage regulators in the system.
65      *
66      * This method should be called when the system is being powered on.  It
67      * needs to occur before the regulators have been enabled/turned on.
68      */
69     void configure() override;
70 
71     /**
72      * Implements the D-Bus "monitor" method.
73      *
74      * Sets whether regulator monitoring is enabled.
75      *
76      * When monitoring is enabled:
77      *   - regulator sensors will be read and published on D-Bus
78      *   - phase fault detection will be performed
79      *
80      * Regulator monitoring should be enabled when the system is being powered
81      * on.  It needs to occur after the regulators have been configured and
82      * enabled/turned on.
83      *
84      * Regulator monitoring should be disabled when the system is being powered
85      * off.  It needs to occur before the regulators have been disabled/turned
86      * off.
87      *
88      * Regulator monitoring can also be temporarily disabled and then re-enabled
89      * while the system is powered on.  This allows other applications or tools
90      * to temporarily communicate with the regulators for testing or debug.
91      * Monitoring should be disabled for only short periods of time; other
92      * applications, such as fan control, may be dependent on regulator sensors.
93      *
94      * @param enable true if monitoring should be enabled, false if it should be
95      *               disabled
96      */
97     void monitor(bool enable) override;
98 
99     /**
100      * Callback that is called when a list of compatible system types is found.
101      *
102      * @param types Compatible system types for the current system ordered from
103      *              most to least specific
104      */
105     void compatibleSystemTypesFound(const std::vector<std::string>& types);
106 
107     /**
108      * Phase fault detection timer expired callback function.
109      */
110     void phaseFaultTimerExpired();
111 
112     /**
113      * Sensor monitoring timer expired callback function.
114      */
115     void sensorTimerExpired();
116 
117     /**
118      * Callback function to handle receiving a HUP signal
119      * to reload the configuration data.
120      *
121      * @param sigSrc sd_event_source signal wrapper
122      * @param sigInfo signal info on signal fd
123      */
124     void sighupHandler(sdeventplus::source::Signal& sigSrc,
125                        const struct signalfd_siginfo* sigInfo);
126 
127   private:
128     /**
129      * Clear any cached data or error history related to hardware devices.
130      *
131      * This method should be called when the system is powering on (booting).
132      * While the system was powered off, hardware could have been added,
133      * removed, or replaced.
134      */
135     void clearHardwareData();
136 
137     /**
138      * Finds the JSON configuration file.
139      *
140      * Looks for a configuration file based on the list of compatible system
141      * types.  If no file is found, looks for a file with the default name.
142      *
143      * Looks for the file in the test directory and standard directory.
144      *
145      * Throws an exception if an operating system error occurs while checking
146      * for the existence of a file.
147      *
148      * @return absolute path to config file, or an empty path if none found
149      */
150     std::filesystem::path findConfigFile();
151 
152     /**
153      * Returns whether the JSON configuration file has been loaded.
154      *
155      * @return true if config file loaded, false otherwise
156      */
isConfigFileLoaded() const157     bool isConfigFileLoaded() const
158     {
159         // If System object exists, the config file has been loaded
160         return (system != nullptr);
161     }
162 
163     /**
164      * Returns whether the system is currently powered on.
165      *
166      * @return true if system is powered on, false otherwise
167      */
168     bool isSystemPoweredOn();
169 
170     /**
171      * Loads the JSON configuration file.
172      *
173      * Looks for the config file using findConfigFile().
174      *
175      * If the config file is found, it is parsed and the resulting information
176      * is stored in the system data member.  If parsing fails, an error is
177      * logged.
178      */
179     void loadConfigFile();
180 
181     /**
182      * Waits until the JSON configuration file has been loaded.
183      *
184      * If the config file has not yet been loaded, waits until one of the
185      * following occurs:
186      * - config file is loaded
187      * - maximum amount of time to wait has elapsed
188      */
189     void waitUntilConfigFileLoaded();
190 
191     /**
192      * The D-Bus bus
193      */
194     sdbusplus::bus_t& bus;
195 
196     /**
197      * Event to loop on
198      */
199     const sdeventplus::Event& eventLoop;
200 
201     /**
202      * System services like error logging and the journal.
203      */
204     BMCServices services;
205 
206     /**
207      * Object that finds the compatible system types for the current system.
208      */
209     std::unique_ptr<util::CompatibleSystemTypesFinder> compatSysTypesFinder;
210 
211     /**
212      * Event timer used to initiate phase fault detection.
213      */
214     Timer phaseFaultTimer;
215 
216     /**
217      * Event timer used to initiate sensor monitoring.
218      */
219     Timer sensorTimer;
220 
221     /**
222      * Indicates whether regulator monitoring is enabled.
223      */
224     bool isMonitoringEnabled{false};
225 
226     /**
227      * List of compatible system types for the current system.
228      *
229      * Used to find the JSON configuration file.
230      */
231     std::vector<std::string> compatibleSystemTypes{};
232 
233     /**
234      * Computer system being controlled and monitored by the BMC.
235      *
236      * Contains the information loaded from the JSON configuration file.
237      * Contains nullptr if the configuration file has not been loaded.
238      */
239     std::unique_ptr<System> system{};
240 };
241 
242 } // namespace phosphor::power::regulators
243