blob: 1763f928ae8f0ffde0eedf043df01f1857d2fbc1 [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 McCarneye0c6a2d2020-05-01 11:37:08 -050018#include "system.hpp"
19
Matthew Barth29e9e382020-01-23 13:40:49 -060020#include <interfaces/manager_interface.hpp>
21#include <sdbusplus/bus.hpp>
Matthew Barth250d0a92020-02-28 13:04:24 -060022#include <sdbusplus/bus/match.hpp>
Matthew Barth29e9e382020-01-23 13:40:49 -060023#include <sdbusplus/server/object.hpp>
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060024#include <sdeventplus/event.hpp>
Matthew Barth7cbc5532020-01-29 15:13:13 -060025#include <sdeventplus/source/signal.hpp>
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060026#include <sdeventplus/utility/timer.hpp>
Matthew Barth29e9e382020-01-23 13:40:49 -060027
Matthew Barthbbc7c582020-02-03 15:55:15 -060028#include <algorithm>
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
37constexpr auto busName = "xyz.openbmc_project.Power.Regulators";
38constexpr auto objPath = "/xyz/openbmc_project/power/regulators/manager";
Matthew Barth23a5e592020-01-30 13:11:08 -060039constexpr auto sysDbusObj = "/xyz/openbmc_project/inventory";
40constexpr auto sysDbusPath = "/xyz/openbmc_project/inventory/system";
41constexpr auto sysDbusIntf = "xyz.openbmc_project.Inventory.Item.System";
42constexpr auto sysDbusProp = "Identifier";
Matthew Barth29e9e382020-01-23 13:40:49 -060043
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060044using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>;
45
Matthew Barth29e9e382020-01-23 13:40:49 -060046using ManagerObject = sdbusplus::server::object::object<
47 phosphor::power::regulators::interface::ManagerInterface>;
48
49class Manager : public ManagerObject
50{
51 public:
52 Manager() = delete;
53 Manager(const Manager&) = delete;
54 Manager(Manager&&) = delete;
55 Manager& operator=(const Manager&) = delete;
56 Manager& operator=(Manager&&) = delete;
57 ~Manager() = default;
58
59 /**
60 * Constructor
61 * Creates a manager over the regulators.
62 *
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050063 * @param bus the dbus bus
64 * @param event the sdevent event
Matthew Barth29e9e382020-01-23 13:40:49 -060065 */
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060066 Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event);
Matthew Barth29e9e382020-01-23 13:40:49 -060067
68 /**
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050069 * Overridden manager object's configure method
Matthew Barth29e9e382020-01-23 13:40:49 -060070 */
71 void configure() override;
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 * Timer expired callback function
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060082 */
83 void timerExpired();
84
Matthew Barth7cbc5532020-01-29 15:13:13 -060085 /**
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050086 * Callback function to handle receiving a HUP signal
Matthew Barth7cbc5532020-01-29 15:13:13 -060087 * to reload the configuration data.
88 *
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050089 * @param sigSrc sd_event_source signal wrapper
90 * @param sigInfo signal info on signal fd
Matthew Barth7cbc5532020-01-29 15:13:13 -060091 */
92 void sighupHandler(sdeventplus::source::Signal& sigSrc,
93 const struct signalfd_siginfo* sigInfo);
94
Matthew Barth250d0a92020-02-28 13:04:24 -060095 /**
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050096 * Callback function to handle interfacesAdded dbus signals
Matthew Barth250d0a92020-02-28 13:04:24 -060097 *
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050098 * @param msg Expanded sdbusplus message data
Matthew Barth250d0a92020-02-28 13:04:24 -060099 */
100 void signalHandler(sdbusplus::message::message& msg);
101
Matthew Barth29e9e382020-01-23 13:40:49 -0600102 private:
103 /**
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500104 * Set the JSON configuration data filename
Matthew Barthbbc7c582020-02-03 15:55:15 -0600105 *
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500106 * @param fName filename without `.json` extension
Matthew Barthbbc7c582020-02-03 15:55:15 -0600107 */
108 inline void setFileName(const std::string& fName)
109 {
110 fileName = fName;
111 if (!fileName.empty())
112 {
113 // Replace all spaces with underscores
114 std::replace(fileName.begin(), fileName.end(), ' ', '_');
115 fileName.append(".json");
116 }
117 };
118
119 /**
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500120 * Get the JSON configuration data filename from dbus
Matthew Barthbbc7c582020-02-03 15:55:15 -0600121 *
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500122 * @return JSON configuration data filename
Matthew Barthbbc7c582020-02-03 15:55:15 -0600123 */
124 const std::string getFileNameDbus();
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500125
126 /**
127 * Finds the JSON configuration file.
128 *
129 * Looks for the config file in the test directory and standard directory.
130 *
131 * Throws an exception if the file cannot be found or a file system error
132 * occurs.
133 *
134 * The base name of the config file must have already been obtained and
135 * stored in the fileName data member.
136 *
137 * @return absolute path to config file
138 */
139 std::filesystem::path findConfigFile();
140
141 /**
142 * Loads the JSON configuration file.
143 *
144 * Looks for the config file in the test directory and standard directory.
145 *
146 * If the config file is found, it is parsed and the resulting information
147 * is stored in the system data member.
148 *
149 * If the config file cannot be found or parsing fails, an error is logged.
150 *
151 * The base name of the config file must have already been obtained and
152 * stored in the fileName data member.
153 */
154 void loadConfigFile();
155
156 /**
157 * The dbus bus
158 */
159 sdbusplus::bus::bus& bus;
160
161 /**
162 * Event to loop on
163 */
164 sdeventplus::Event eventLoop;
165
166 /**
167 * List of event timers
168 */
169 std::vector<Timer> timers{};
170
171 /**
172 * List of dbus signal matches
173 */
174 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> signals{};
175
176 /**
177 * JSON configuration file base name.
178 */
179 std::string fileName{};
180
181 /**
182 * Computer system being controlled and monitored by the BMC.
183 *
184 * Contains the information loaded from the JSON configuration file.
185 * Contains nullptr if the configuration file has not been loaded.
186 */
187 std::unique_ptr<System> system{};
Matthew Barth29e9e382020-01-23 13:40:49 -0600188};
189
190} // namespace phosphor::power::regulators