blob: 51c8944a2d25e03c36ded0a5be6d5e4bbfefcf48 [file] [log] [blame]
Matthew Barth29e9e382020-01-23 13:40:49 -06001/**
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
17#include "manager.hpp"
18
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050019#include "chassis.hpp"
20#include "config_file_parser.hpp"
21#include "exception_utils.hpp"
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050022#include "rule.hpp"
Matthew Barthbbc7c582020-02-03 15:55:15 -060023#include "utility.hpp"
24
Shawn McCarney589c1812021-01-14 12:13:26 -060025#include <algorithm>
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060026#include <chrono>
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050027#include <exception>
Shawn McCarney589c1812021-01-14 12:13:26 -060028#include <functional>
29#include <map>
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050030#include <tuple>
31#include <utility>
Matthew Barth250d0a92020-02-28 13:04:24 -060032#include <variant>
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060033
Shawn McCarney84807b92020-04-30 18:40:03 -050034namespace phosphor::power::regulators
Matthew Barth29e9e382020-01-23 13:40:49 -060035{
36
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050037namespace fs = std::filesystem;
38
Shawn McCarney589c1812021-01-14 12:13:26 -060039constexpr auto busName = "xyz.openbmc_project.Power.Regulators";
40constexpr auto managerObjPath = "/xyz/openbmc_project/power/regulators/manager";
41constexpr auto compatibleIntf =
42 "xyz.openbmc_project.Configuration.IBMCompatibleSystem";
43constexpr auto compatibleNamesProp = "Names";
44
45/**
46 * Default configuration file name. This is used when the system does not
47 * implement the D-Bus compatible interface.
48 */
49constexpr auto defaultConfigFileName = "config.json";
50
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050051/**
52 * Standard configuration file directory. This directory is part of the
53 * firmware install image. It contains the standard version of the config file.
54 */
55const fs::path standardConfigFileDir{"/usr/share/phosphor-regulators"};
56
57/**
58 * Test configuration file directory. This directory can contain a test version
59 * of the config file. The test version will override the standard version.
60 */
61const fs::path testConfigFileDir{"/etc/phosphor-regulators"};
62
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060063Manager::Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event) :
Shawn McCarney589c1812021-01-14 12:13:26 -060064 ManagerObject{bus, managerObjPath, true}, bus{bus}, eventLoop{event},
65 services{bus}
Matthew Barth29e9e382020-01-23 13:40:49 -060066{
Shawn McCarney589c1812021-01-14 12:13:26 -060067 // Subscribe to D-Bus interfacesAdded signal from Entity Manager. This
68 // notifies us if the compatible interface becomes available later.
69 std::string matchStr = sdbusplus::bus::match::rules::interfacesAdded() +
70 sdbusplus::bus::match::rules::sender(
71 "xyz.openbmc_project.EntityManager");
72 std::unique_ptr<sdbusplus::server::match::match> matchPtr =
73 std::make_unique<sdbusplus::server::match::match>(
74 bus, matchStr,
75 std::bind(&Manager::interfacesAddedHandler, this,
76 std::placeholders::_1));
77 signals.emplace_back(std::move(matchPtr));
Matthew Barth250d0a92020-02-28 13:04:24 -060078
Shawn McCarney589c1812021-01-14 12:13:26 -060079 // Try to find compatible system types using D-Bus compatible interface.
80 // Note that it might not be supported on this system, or the service that
81 // provides the interface might not be running yet.
82 findCompatibleSystemTypes();
Shawn McCarney84807b92020-04-30 18:40:03 -050083
Shawn McCarney589c1812021-01-14 12:13:26 -060084 // Try to find and load the JSON configuration file
85 loadConfigFile();
Matthew Barth29e9e382020-01-23 13:40:49 -060086
87 // Obtain dbus service name
88 bus.request_name(busName);
89}
90
91void Manager::configure()
92{
Shawn McCarney6345c6c2020-05-04 10:35:05 -050093 // Verify System object exists; this means config file has been loaded
94 if (system)
95 {
96 // Configure the regulator devices in the system
Bob King23243f82020-07-29 10:38:57 +080097 system->configure(services);
Shawn McCarney6345c6c2020-05-04 10:35:05 -050098 }
99 else
100 {
Shawn McCarneyb464c8b2020-07-16 17:51:38 -0500101 services.getJournal().logError("Unable to configure regulator devices: "
102 "Configuration file not loaded");
Shawn McCarney6345c6c2020-05-04 10:35:05 -0500103 // TODO: Log error
104 }
105
Matthew Barth29e9e382020-01-23 13:40:49 -0600106 // TODO Configuration errors that should halt poweron,
107 // throw InternalFailure exception (or similar) to
108 // fail the call(busctl) to this method
109}
110
Shawn McCarney589c1812021-01-14 12:13:26 -0600111void Manager::interfacesAddedHandler(sdbusplus::message::message& msg)
112{
113 // Verify message is valid
114 if (!msg)
115 {
116 return;
117 }
118
119 try
120 {
121 // Read object path for object that was created or had interface added
122 sdbusplus::message::object_path objPath;
123 msg.read(objPath);
124
125 // Read the dictionary whose keys are interface names and whose values
126 // are dictionaries containing the interface property names and values
127 std::map<std::string,
128 std::map<std::string, std::variant<std::vector<std::string>>>>
129 intfProp;
130 msg.read(intfProp);
131
132 // Find the compatible interface, if present
133 auto itIntf = intfProp.find(compatibleIntf);
134 if (itIntf != intfProp.cend())
135 {
136 // Find the Names property of the compatible interface, if present
137 auto itProp = itIntf->second.find(compatibleNamesProp);
138 if (itProp != itIntf->second.cend())
139 {
140 // Get value of Names property
141 auto propValue = std::get<0>(itProp->second);
142 if (!propValue.empty())
143 {
144 // Store list of compatible system types
145 compatibleSystemTypes = propValue;
146
147 // Find and load JSON config file based on system types
148 loadConfigFile();
149 }
150 }
151 }
152 }
153 catch (const std::exception&)
154 {
155 // Error trying to read interfacesAdded message. One possible cause
156 // could be a property whose value is not a std::vector<std::string>.
157 }
158}
159
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500160void Manager::monitor(bool enable)
Matthew Barth29e9e382020-01-23 13:40:49 -0600161{
162 if (enable)
163 {
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500164 /* Temporarily comment out until monitoring is supported.
165 Timer timer(eventLoop, std::bind(&Manager::timerExpired, this));
166 // Set timer as a repeating 1sec timer
167 timer.restart(std::chrono::milliseconds(1000));
168 timers.emplace_back(std::move(timer));
169 */
Matthew Barth29e9e382020-01-23 13:40:49 -0600170 }
171 else
172 {
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500173 /* Temporarily comment out until monitoring is supported.
174 // Delete all timers to disable monitoring
175 timers.clear();
176 */
177
178 // Verify System object exists; this means config file has been loaded
179 if (system)
180 {
181 // Close the regulator devices in the system. Monitoring is
182 // normally disabled because the system is being powered off. The
183 // devices should be closed in case hardware is removed or replaced
184 // while the system is at standby.
Bob Kingd692d6d2020-09-14 13:42:57 +0800185 system->closeDevices(services);
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500186 }
Matthew Barth29e9e382020-01-23 13:40:49 -0600187 }
188}
189
Shawn McCarney589c1812021-01-14 12:13:26 -0600190void Manager::sighupHandler(sdeventplus::source::Signal& /*sigSrc*/,
191 const struct signalfd_siginfo* /*sigInfo*/)
192{
193 // Reload the JSON configuration file
194 loadConfigFile();
195}
196
Matthew Barthf2bcf1f2020-01-29 14:42:47 -0600197void Manager::timerExpired()
198{
199 // TODO Analyze, refresh sensor status, and
200 // collect/update telemetry for each regulator
201}
202
Shawn McCarney589c1812021-01-14 12:13:26 -0600203void Manager::findCompatibleSystemTypes()
Matthew Barth7cbc5532020-01-29 15:13:13 -0600204{
Matthew Barthbbc7c582020-02-03 15:55:15 -0600205 using namespace phosphor::power::util;
206
207 try
208 {
Shawn McCarney589c1812021-01-14 12:13:26 -0600209 // Query object mapper for object paths that implement the compatible
210 // interface. Returns a map of object paths to a map of services names
211 // to their interfaces.
212 DbusSubtree subTree = getSubTree(bus, "/xyz/openbmc_project/inventory",
213 compatibleIntf, 0);
214
215 // Get the first object path
216 auto objectIt = subTree.cbegin();
217 if (objectIt != subTree.cend())
Matthew Barthbbc7c582020-02-03 15:55:15 -0600218 {
Shawn McCarney589c1812021-01-14 12:13:26 -0600219 std::string objPath = objectIt->first;
220
221 // Get the first service name
222 auto serviceIt = objectIt->second.cbegin();
223 if (serviceIt != objectIt->second.cend())
224 {
225 std::string service = serviceIt->first;
226 if (!service.empty())
227 {
228 // Get compatible system types property value
229 getProperty(compatibleIntf, compatibleNamesProp, objPath,
230 service, bus, compatibleSystemTypes);
231 }
232 }
Matthew Barthbbc7c582020-02-03 15:55:15 -0600233 }
234 }
Shawn McCarney589c1812021-01-14 12:13:26 -0600235 catch (const std::exception&)
Matthew Barthbbc7c582020-02-03 15:55:15 -0600236 {
Shawn McCarney589c1812021-01-14 12:13:26 -0600237 // Compatible system types information is not available. The current
238 // system might not support the interface, or the service that
239 // implements the interface might not be running yet.
Matthew Barthbbc7c582020-02-03 15:55:15 -0600240 }
Matthew Barthbbc7c582020-02-03 15:55:15 -0600241}
242
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500243fs::path Manager::findConfigFile()
244{
Shawn McCarney589c1812021-01-14 12:13:26 -0600245 // Build list of possible base file names
246 std::vector<std::string> fileNames{};
247
248 // Add possible file names based on compatible system types (if any)
249 for (const std::string& systemType : compatibleSystemTypes)
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500250 {
Shawn McCarney589c1812021-01-14 12:13:26 -0600251 // Replace all spaces and commas in system type name with underscores
252 std::string fileName{systemType};
253 std::replace(fileName.begin(), fileName.end(), ' ', '_');
254 std::replace(fileName.begin(), fileName.end(), ',', '_');
255
256 // Append .json suffix and add to list
257 fileName.append(".json");
258 fileNames.emplace_back(fileName);
259 }
260
261 // Add default file name for systems that don't use compatible interface
262 fileNames.emplace_back(defaultConfigFileName);
263
264 // Look for a config file with one of the possible base names
265 for (const std::string& fileName : fileNames)
266 {
267 // Check if file exists in test directory
268 fs::path pathName{testConfigFileDir / fileName};
269 if (fs::exists(pathName))
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500270 {
Shawn McCarney589c1812021-01-14 12:13:26 -0600271 return pathName;
272 }
273
274 // Check if file exists in standard directory
275 pathName = standardConfigFileDir / fileName;
276 if (fs::exists(pathName))
277 {
278 return pathName;
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500279 }
280 }
281
Shawn McCarney589c1812021-01-14 12:13:26 -0600282 // No config file found; return empty path
283 return fs::path{};
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500284}
285
286void Manager::loadConfigFile()
287{
288 try
289 {
290 // Find the absolute path to the config file
291 fs::path pathName = findConfigFile();
Shawn McCarney589c1812021-01-14 12:13:26 -0600292 if (!pathName.empty())
293 {
294 // Log info message in journal; config file path is important
295 services.getJournal().logInfo("Loading configuration file " +
296 pathName.string());
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500297
Shawn McCarney589c1812021-01-14 12:13:26 -0600298 // Parse the config file
299 std::vector<std::unique_ptr<Rule>> rules{};
300 std::vector<std::unique_ptr<Chassis>> chassis{};
301 std::tie(rules, chassis) = config_file_parser::parse(pathName);
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500302
Shawn McCarney589c1812021-01-14 12:13:26 -0600303 // Store config file information in a new System object. The old
304 // System object, if any, is automatically deleted.
305 system =
306 std::make_unique<System>(std::move(rules), std::move(chassis));
307 }
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500308 }
309 catch (const std::exception& e)
310 {
311 // Log error messages in journal
Shawn McCarneyb464c8b2020-07-16 17:51:38 -0500312 services.getJournal().logError(exception_utils::getMessages(e));
313 services.getJournal().logError("Unable to load configuration file");
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500314
315 // TODO: Create error log entry
316 }
317}
318
Shawn McCarney84807b92020-04-30 18:40:03 -0500319} // namespace phosphor::power::regulators