blob: d9b62a0e9b7b0f5a812ee20e8a9623df69b56e65 [file] [log] [blame]
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -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 */
Matthew Barth29e9e382020-01-23 13:40:49 -060016#pragma once
17
Shawn McCarneyb464c8b2020-07-16 17:51:38 -050018#include "services.hpp"
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050019#include "system.hpp"
20
Matthew Barth29e9e382020-01-23 13:40:49 -060021#include <interfaces/manager_interface.hpp>
22#include <sdbusplus/bus.hpp>
Matthew Barth250d0a92020-02-28 13:04:24 -060023#include <sdbusplus/bus/match.hpp>
Matthew Barth29e9e382020-01-23 13:40:49 -060024#include <sdbusplus/server/object.hpp>
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060025#include <sdeventplus/event.hpp>
Matthew Barth7cbc5532020-01-29 15:13:13 -060026#include <sdeventplus/source/signal.hpp>
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060027#include <sdeventplus/utility/timer.hpp>
Matthew Barth29e9e382020-01-23 13:40:49 -060028
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050029#include <filesystem>
30#include <memory>
31#include <string>
32#include <vector>
Matthew Barthbbc7c582020-02-03 15:55:15 -060033
Matthew Barth29e9e382020-01-23 13:40:49 -060034namespace phosphor::power::regulators
35{
36
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060037using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>;
38
Matthew Barth29e9e382020-01-23 13:40:49 -060039using ManagerObject = sdbusplus::server::object::object<
40 phosphor::power::regulators::interface::ManagerInterface>;
41
42class 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 *
Shawn McCarneyb464c8b2020-07-16 17:51:38 -050056 * @param bus the D-Bus bus
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050057 * @param event the sdevent event
Matthew Barth29e9e382020-01-23 13:40:49 -060058 */
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060059 Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event);
Matthew Barth29e9e382020-01-23 13:40:49 -060060
61 /**
Shawn McCarneyd9c8be52021-05-18 10:07:53 -050062 * 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.
Matthew Barth29e9e382020-01-23 13:40:49 -060068 */
69 void configure() override;
70
71 /**
Shawn McCarney589c1812021-01-14 12:13:26 -060072 * Callback function to handle interfacesAdded D-Bus signals
73 *
74 * @param msg Expanded sdbusplus message data
75 */
76 void interfacesAddedHandler(sdbusplus::message::message& msg);
77
78 /**
Shawn McCarneyd9c8be52021-05-18 10:07:53 -050079 * Implements the D-Bus "monitor" method.
Matthew Barth29e9e382020-01-23 13:40:49 -060080 *
Shawn McCarneyd9c8be52021-05-18 10:07:53 -050081 * Sets whether regulator monitoring is enabled.
82 *
83 * When monitoring is enabled:
84 * - regulator sensors will be read and published on D-Bus
85 * - phase fault detection will be performed
86 *
87 * Regulator monitoring should be enabled when the system is being powered
88 * on. It needs to occur after the regulators have been configured and
89 * enabled/turned on.
90 *
91 * Regulator monitoring should be disabled when the system is being powered
92 * off. It needs to occur before the regulators have been disabled/turned
93 * off.
94 *
95 * Regulator monitoring can also be temporarily disabled and then re-enabled
96 * while the system is powered on. This allows other applications or tools
97 * to temporarily communicate with the regulators for testing or debug.
98 * Monitoring should be disabled for only short periods of time; other
99 * applications, such as fan control, may be dependent on regulator sensors.
100 *
101 * @param enable true if monitoring should be enabled, false if it should be
102 * disabled
Matthew Barth29e9e382020-01-23 13:40:49 -0600103 */
104 void monitor(bool enable) override;
105
Matthew Barthf2bcf1f2020-01-29 14:42:47 -0600106 /**
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500107 * Callback function to handle receiving a HUP signal
Matthew Barth7cbc5532020-01-29 15:13:13 -0600108 * to reload the configuration data.
109 *
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500110 * @param sigSrc sd_event_source signal wrapper
111 * @param sigInfo signal info on signal fd
Matthew Barth7cbc5532020-01-29 15:13:13 -0600112 */
113 void sighupHandler(sdeventplus::source::Signal& sigSrc,
114 const struct signalfd_siginfo* sigInfo);
115
Matthew Barth250d0a92020-02-28 13:04:24 -0600116 /**
Shawn McCarney589c1812021-01-14 12:13:26 -0600117 * Timer expired callback function
Matthew Barth250d0a92020-02-28 13:04:24 -0600118 */
Shawn McCarney589c1812021-01-14 12:13:26 -0600119 void timerExpired();
Matthew Barth250d0a92020-02-28 13:04:24 -0600120
Matthew Barth29e9e382020-01-23 13:40:49 -0600121 private:
122 /**
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600123 * Clear any cached data or error history related to hardware devices.
124 *
125 * This method should be called when the system is powering on (booting).
126 * While the system was powered off, hardware could have been added,
127 * removed, or replaced.
128 */
129 void clearHardwareData();
130
131 /**
Shawn McCarney589c1812021-01-14 12:13:26 -0600132 * Finds the list of compatible system types using D-Bus methods.
Matthew Barthbbc7c582020-02-03 15:55:15 -0600133 *
Shawn McCarney589c1812021-01-14 12:13:26 -0600134 * This list is used to find the correct JSON configuration file for the
135 * current system.
Matthew Barthbbc7c582020-02-03 15:55:15 -0600136 *
Shawn McCarney589c1812021-01-14 12:13:26 -0600137 * Note that some systems do not support the D-Bus compatible interface.
138 *
139 * If a list of compatible system types is found, it is stored in the
140 * compatibleSystemTypes data member.
Matthew Barthbbc7c582020-02-03 15:55:15 -0600141 */
Shawn McCarney589c1812021-01-14 12:13:26 -0600142 void findCompatibleSystemTypes();
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500143
144 /**
145 * Finds the JSON configuration file.
146 *
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500147 * Looks for a configuration file based on the list of compatible system
Shawn McCarney589c1812021-01-14 12:13:26 -0600148 * types. If no file is found, looks for a file with the default name.
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500149 *
Shawn McCarney589c1812021-01-14 12:13:26 -0600150 * Looks for the file in the test directory and standard directory.
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500151 *
Shawn McCarney589c1812021-01-14 12:13:26 -0600152 * Throws an exception if an operating system error occurs while checking
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500153 * for the existence of a file.
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500154 *
Shawn McCarney589c1812021-01-14 12:13:26 -0600155 * @return absolute path to config file, or an empty path if none found
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500156 */
157 std::filesystem::path findConfigFile();
158
159 /**
Shawn McCarney8acaf542021-03-30 10:38:37 -0500160 * Returns whether the JSON configuration file has been loaded.
161 *
162 * @return true if config file loaded, false otherwise
163 */
164 bool isConfigFileLoaded() const
165 {
166 // If System object exists, the config file has been loaded
167 return (system != nullptr);
168 }
169
170 /**
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500171 * Returns whether the system is currently powered on.
172 *
173 * @return true if system is powered on, false otherwise
174 */
175 bool isSystemPoweredOn();
176
177 /**
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500178 * Loads the JSON configuration file.
179 *
Shawn McCarney589c1812021-01-14 12:13:26 -0600180 * Looks for the config file using findConfigFile().
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500181 *
182 * If the config file is found, it is parsed and the resulting information
Shawn McCarney589c1812021-01-14 12:13:26 -0600183 * is stored in the system data member. If parsing fails, an error is
184 * logged.
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500185 */
186 void loadConfigFile();
187
188 /**
Shawn McCarney8acaf542021-03-30 10:38:37 -0500189 * Waits until the JSON configuration file has been loaded.
190 *
191 * If the config file has not yet been loaded, waits until one of the
192 * following occurs:
193 * - config file is loaded
194 * - maximum amount of time to wait has elapsed
195 */
196 void waitUntilConfigFileLoaded();
197
198 /**
Shawn McCarneyb464c8b2020-07-16 17:51:38 -0500199 * The D-Bus bus
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500200 */
201 sdbusplus::bus::bus& bus;
202
203 /**
204 * Event to loop on
205 */
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500206 const sdeventplus::Event& eventLoop;
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500207
208 /**
Shawn McCarneyb464c8b2020-07-16 17:51:38 -0500209 * System services like error logging and the journal.
210 */
211 BMCServices services;
212
213 /**
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500214 * Event timer used to initiate regulator monitoring.
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500215 */
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500216 Timer timer;
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500217
218 /**
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500219 * List of D-Bus signal matches
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500220 */
221 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> signals{};
222
223 /**
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500224 * Indicates whether regulator monitoring is enabled.
225 */
226 bool isMonitoringEnabled{false};
227
228 /**
Shawn McCarney589c1812021-01-14 12:13:26 -0600229 * List of compatible system types for the current system.
230 *
231 * Used to find the JSON configuration file.
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500232 */
Shawn McCarney589c1812021-01-14 12:13:26 -0600233 std::vector<std::string> compatibleSystemTypes{};
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500234
235 /**
236 * Computer system being controlled and monitored by the BMC.
237 *
238 * Contains the information loaded from the JSON configuration file.
239 * Contains nullptr if the configuration file has not been loaded.
240 */
241 std::unique_ptr<System> system{};
Matthew Barth29e9e382020-01-23 13:40:49 -0600242};
243
244} // namespace phosphor::power::regulators