blob: e2ec5a78343f3511d61090ae01b4dab1398d2d0c [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 McCarneye0c6a2d2020-05-01 11:37:08 -050062 * Overridden manager object's configure method
Matthew Barth29e9e382020-01-23 13:40:49 -060063 */
64 void configure() override;
65
66 /**
Shawn McCarney589c1812021-01-14 12:13:26 -060067 * Callback function to handle interfacesAdded D-Bus signals
68 *
69 * @param msg Expanded sdbusplus message data
70 */
71 void interfacesAddedHandler(sdbusplus::message::message& msg);
72
73 /**
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050074 * Overridden manager object's monitor method
Matthew Barth29e9e382020-01-23 13:40:49 -060075 *
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050076 * @param enable Enable or disable regulator monitoring
Matthew Barth29e9e382020-01-23 13:40:49 -060077 */
78 void monitor(bool enable) override;
79
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060080 /**
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050081 * Callback function to handle receiving a HUP signal
Matthew Barth7cbc5532020-01-29 15:13:13 -060082 * to reload the configuration data.
83 *
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050084 * @param sigSrc sd_event_source signal wrapper
85 * @param sigInfo signal info on signal fd
Matthew Barth7cbc5532020-01-29 15:13:13 -060086 */
87 void sighupHandler(sdeventplus::source::Signal& sigSrc,
88 const struct signalfd_siginfo* sigInfo);
89
Matthew Barth250d0a92020-02-28 13:04:24 -060090 /**
Shawn McCarney589c1812021-01-14 12:13:26 -060091 * Timer expired callback function
Matthew Barth250d0a92020-02-28 13:04:24 -060092 */
Shawn McCarney589c1812021-01-14 12:13:26 -060093 void timerExpired();
Matthew Barth250d0a92020-02-28 13:04:24 -060094
Matthew Barth29e9e382020-01-23 13:40:49 -060095 private:
96 /**
Shawn McCarney9bd94d32021-01-25 19:40:42 -060097 * Clear any cached data or error history related to hardware devices.
98 *
99 * This method should be called when the system is powering on (booting).
100 * While the system was powered off, hardware could have been added,
101 * removed, or replaced.
102 */
103 void clearHardwareData();
104
105 /**
Shawn McCarney589c1812021-01-14 12:13:26 -0600106 * Finds the list of compatible system types using D-Bus methods.
Matthew Barthbbc7c582020-02-03 15:55:15 -0600107 *
Shawn McCarney589c1812021-01-14 12:13:26 -0600108 * This list is used to find the correct JSON configuration file for the
109 * current system.
Matthew Barthbbc7c582020-02-03 15:55:15 -0600110 *
Shawn McCarney589c1812021-01-14 12:13:26 -0600111 * Note that some systems do not support the D-Bus compatible interface.
112 *
113 * If a list of compatible system types is found, it is stored in the
114 * compatibleSystemTypes data member.
Matthew Barthbbc7c582020-02-03 15:55:15 -0600115 */
Shawn McCarney589c1812021-01-14 12:13:26 -0600116 void findCompatibleSystemTypes();
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500117
118 /**
119 * Finds the JSON configuration file.
120 *
Shawn McCarney589c1812021-01-14 12:13:26 -0600121 * Looks for a configuration file based on the list of compatable system
122 * types. If no file is found, looks for a file with the default name.
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500123 *
Shawn McCarney589c1812021-01-14 12:13:26 -0600124 * Looks for the file in the test directory and standard directory.
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500125 *
Shawn McCarney589c1812021-01-14 12:13:26 -0600126 * Throws an exception if an operating system error occurs while checking
127 * for the existance of a file.
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500128 *
Shawn McCarney589c1812021-01-14 12:13:26 -0600129 * @return absolute path to config file, or an empty path if none found
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500130 */
131 std::filesystem::path findConfigFile();
132
133 /**
Shawn McCarney8acaf542021-03-30 10:38:37 -0500134 * Returns whether the JSON configuration file has been loaded.
135 *
136 * @return true if config file loaded, false otherwise
137 */
138 bool isConfigFileLoaded() const
139 {
140 // If System object exists, the config file has been loaded
141 return (system != nullptr);
142 }
143
144 /**
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500145 * Loads the JSON configuration file.
146 *
Shawn McCarney589c1812021-01-14 12:13:26 -0600147 * Looks for the config file using findConfigFile().
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500148 *
149 * If the config file is found, it is parsed and the resulting information
Shawn McCarney589c1812021-01-14 12:13:26 -0600150 * is stored in the system data member. If parsing fails, an error is
151 * logged.
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500152 */
153 void loadConfigFile();
154
155 /**
Shawn McCarney8acaf542021-03-30 10:38:37 -0500156 * Waits until the JSON configuration file has been loaded.
157 *
158 * If the config file has not yet been loaded, waits until one of the
159 * following occurs:
160 * - config file is loaded
161 * - maximum amount of time to wait has elapsed
162 */
163 void waitUntilConfigFileLoaded();
164
165 /**
Shawn McCarneyb464c8b2020-07-16 17:51:38 -0500166 * The D-Bus bus
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500167 */
168 sdbusplus::bus::bus& bus;
169
170 /**
171 * Event to loop on
172 */
173 sdeventplus::Event eventLoop;
174
175 /**
Shawn McCarneyb464c8b2020-07-16 17:51:38 -0500176 * System services like error logging and the journal.
177 */
178 BMCServices services;
179
180 /**
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500181 * List of event timers
182 */
183 std::vector<Timer> timers{};
184
185 /**
186 * List of dbus signal matches
187 */
188 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> signals{};
189
190 /**
Shawn McCarney589c1812021-01-14 12:13:26 -0600191 * List of compatible system types for the current system.
192 *
193 * Used to find the JSON configuration file.
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500194 */
Shawn McCarney589c1812021-01-14 12:13:26 -0600195 std::vector<std::string> compatibleSystemTypes{};
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500196
197 /**
198 * Computer system being controlled and monitored by the BMC.
199 *
200 * Contains the information loaded from the JSON configuration file.
201 * Contains nullptr if the configuration file has not been loaded.
202 */
203 std::unique_ptr<System> system{};
Matthew Barth29e9e382020-01-23 13:40:49 -0600204};
205
206} // namespace phosphor::power::regulators