blob: e09317b8888089278ad14f38f25a4d74a9201968 [file] [log] [blame]
Matthew Barthfd05d642019-11-14 15:01:57 -06001/**
2 * Copyright © 2019 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 */
Jolie Ku1a568652020-08-24 16:32:15 +080016#include "json_parser.hpp"
Matthew Barth2d2caa32020-05-26 11:07:24 -050017
18#include "anyof.hpp"
19#include "fallback.hpp"
20#include "gpio.hpp"
Jolie Ku1a568652020-08-24 16:32:15 +080021#include "json_config.hpp"
Matthew Barth2d2caa32020-05-26 11:07:24 -050022#include "sdbusplus.hpp"
23#include "tach.hpp"
24
Matthew Barth0961fae2019-11-15 09:00:27 -060025#include <nlohmann/json.hpp>
26#include <phosphor-logging/log.hpp>
Matthew Barth5060b102019-12-16 10:46:35 -060027#include <sdbusplus/bus.hpp>
Mike Cappsa35a8902021-06-10 10:20:14 -040028#include <xyz/openbmc_project/Logging/Create/server.hpp>
29#include <xyz/openbmc_project/Logging/Entry/server.hpp>
Matthew Barthfd05d642019-11-14 15:01:57 -060030
Matthew Barth2d2caa32020-05-26 11:07:24 -050031#include <filesystem>
32#include <fstream>
33#include <string>
Matthew Barthfd05d642019-11-14 15:01:57 -060034
35namespace phosphor
36{
37namespace fan
38{
39namespace presence
40{
41
Matthew Barth0961fae2019-11-15 09:00:27 -060042using json = nlohmann::json;
43namespace fs = std::filesystem;
44using namespace phosphor::logging;
45
Matthew Barthfd05d642019-11-14 15:01:57 -060046policies JsonConfig::_policies;
Matthew Barth2d2caa32020-05-26 11:07:24 -050047const std::map<std::string, methodHandler> JsonConfig::_methods = {
48 {"tach", method::getTach}, {"gpio", method::getGpio}};
49const std::map<std::string, rpolicyHandler> JsonConfig::_rpolicies = {
50 {"anyof", rpolicy::getAnyof}, {"fallback", rpolicy::getFallback}};
Matthew Barthfd05d642019-11-14 15:01:57 -060051
Mike Cappsa35a8902021-06-10 10:20:14 -040052const auto loggingPath = "/xyz/openbmc_project/logging";
53const auto loggingCreateIface = "xyz.openbmc_project.Logging.Create";
54
Matthew Barth2d2caa32020-05-26 11:07:24 -050055JsonConfig::JsonConfig(sdbusplus::bus::bus& bus) : _bus(bus)
Matt Spinler0daedd12021-01-25 14:46:03 -060056{}
Jolie Ku1a568652020-08-24 16:32:15 +080057
Matthew Barth5b839912021-06-21 14:46:34 -050058void JsonConfig::start()
Matt Spinler0daedd12021-01-25 14:46:03 -060059{
Matthew Barth5b839912021-06-21 14:46:34 -050060 using config = fan::JsonConfig;
61
Matt Spinlerdfc8c4d2022-06-22 16:52:27 -050062 if (!_loaded)
Matt Spinler0daedd12021-01-25 14:46:03 -060063 {
Matt Spinlerdfc8c4d2022-06-22 16:52:27 -050064 process(
65 config::load(config::getConfFile(_bus, confAppName, confFileName)));
66
67 _loaded = true;
68
69 for (auto& p : _policies)
70 {
71 p->monitor();
72 }
Matt Spinler0daedd12021-01-25 14:46:03 -060073 }
Matthew Barthfd05d642019-11-14 15:01:57 -060074}
75
76const policies& JsonConfig::get()
77{
78 return _policies;
79}
80
Matthew Barthf3e70472019-12-03 13:33:20 -060081void JsonConfig::sighupHandler(sdeventplus::source::Signal& sigSrc,
82 const struct signalfd_siginfo* sigInfo)
83{
84 try
85 {
Jolie Ku1a568652020-08-24 16:32:15 +080086 using config = fan::JsonConfig;
87
Matt Spinler9e9f5992020-09-30 08:29:24 -050088 _reporter.reset();
89
Matthew Barthf3e70472019-12-03 13:33:20 -060090 // Load and process the json configuration
Jolie Ku1a568652020-08-24 16:32:15 +080091 process(
92 config::load(config::getConfFile(_bus, confAppName, confFileName)));
93
Matthew Barth2d2caa32020-05-26 11:07:24 -050094 for (auto& p : _policies)
Matthew Barthf3e70472019-12-03 13:33:20 -060095 {
96 p->monitor();
97 }
98 log<level::INFO>("Configuration loaded successfully");
99 }
Patrick Williamsddb773b2021-10-06 11:24:49 -0500100 catch (const std::runtime_error& re)
Matthew Barthf3e70472019-12-03 13:33:20 -0600101 {
102 log<level::ERR>("Error loading config, no config changes made",
103 entry("LOAD_ERROR=%s", re.what()));
104 }
105}
106
Matthew Barth4a94dec2019-11-15 10:40:47 -0600107void JsonConfig::process(const json& jsonConf)
108{
Matthew Barthf3e70472019-12-03 13:33:20 -0600109 policies policies;
110 std::vector<fanPolicy> fans;
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600111 // Set the expected number of fan entries
112 // to be size of the list of fan json config entries
113 // (Must be done to eliminate vector reallocation of fan references)
Matt Spinler9e9f5992020-09-30 08:29:24 -0500114 fans.reserve(jsonConf.size());
115 for (auto& member : jsonConf)
Matthew Barth4a94dec2019-11-15 10:40:47 -0600116 {
Matthew Barthe7566632019-11-18 16:13:04 -0600117 if (!member.contains("name") || !member.contains("path") ||
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600118 !member.contains("methods") || !member.contains("rpolicy"))
Matthew Barth4a94dec2019-11-15 10:40:47 -0600119 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500120 log<level::ERR>("Missing required fan presence properties",
121 entry("REQUIRED_PROPERTIES=%s",
122 "{name, path, methods, rpolicy}"));
Matthew Barth4a94dec2019-11-15 10:40:47 -0600123 throw std::runtime_error(
124 "Missing required fan presence properties");
125 }
Matthew Barthe7566632019-11-18 16:13:04 -0600126
127 // Loop thru the configured methods of presence detection
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600128 std::vector<std::unique_ptr<PresenceSensor>> sensors;
Matthew Barthe7566632019-11-18 16:13:04 -0600129 for (auto& method : member["methods"].items())
130 {
131 if (!method.value().contains("type"))
132 {
133 log<level::ERR>(
134 "Missing required fan presence method type",
135 entry("FAN_NAME=%s",
Matthew Barth2d2caa32020-05-26 11:07:24 -0500136 member["name"].get<std::string>().c_str()));
Matthew Barthe7566632019-11-18 16:13:04 -0600137 throw std::runtime_error(
138 "Missing required fan presence method type");
139 }
140 // The method type of fan presence detection
141 // (Must have a supported function within the method namespace)
142 auto type = method.value()["type"].get<std::string>();
143 std::transform(type.begin(), type.end(), type.begin(), tolower);
144 auto func = _methods.find(type);
145 if (func != _methods.end())
146 {
147 // Call function for method type
Matthew Barthf3e70472019-12-03 13:33:20 -0600148 auto sensor = func->second(fans.size(), method.value());
Matthew Barthe7566632019-11-18 16:13:04 -0600149 if (sensor)
150 {
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600151 sensors.emplace_back(std::move(sensor));
Matthew Barthe7566632019-11-18 16:13:04 -0600152 }
153 }
154 else
155 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500156 log<level::ERR>(
157 "Invalid fan presence method type",
158 entry("FAN_NAME=%s",
159 member["name"].get<std::string>().c_str()),
160 entry("METHOD_TYPE=%s", type.c_str()));
Matthew Barthe7566632019-11-18 16:13:04 -0600161 throw std::runtime_error("Invalid fan presence method type");
162 }
163 }
Matt Spinler9e9f5992020-09-30 08:29:24 -0500164
165 // Get the amount of time a fan must be not present before
166 // creating an error.
167 std::optional<size_t> timeUntilError;
168 if (member.contains("fan_missing_error_time"))
169 {
170 timeUntilError = member["fan_missing_error_time"].get<size_t>();
171 }
172
173 auto fan =
174 std::make_tuple(member["name"], member["path"], timeUntilError);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600175 // Create a fan object
Matthew Barthf3e70472019-12-03 13:33:20 -0600176 fans.emplace_back(std::make_tuple(fan, std::move(sensors)));
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600177
178 // Add fan presence policy
Matthew Barthf3e70472019-12-03 13:33:20 -0600179 auto policy = getPolicy(member["rpolicy"], fans.back());
180 if (policy)
181 {
182 policies.emplace_back(std::move(policy));
183 }
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600184 }
Matthew Barthf3e70472019-12-03 13:33:20 -0600185
186 // Success, refresh fans and policies lists
187 _fans.clear();
188 _fans.swap(fans);
189
190 _policies.clear();
191 _policies.swap(policies);
Matt Spinlere8122392020-09-24 13:22:18 -0500192
193 // Create the error reporter class if necessary
Matt Spinler9e9f5992020-09-30 08:29:24 -0500194 if (std::any_of(_fans.begin(), _fans.end(), [](const auto& fan) {
195 return std::get<std::optional<size_t>>(std::get<Fan>(fan)) !=
196 std::nullopt;
197 }))
Matt Spinlere8122392020-09-24 13:22:18 -0500198 {
Matt Spinler9e9f5992020-09-30 08:29:24 -0500199 _reporter = std::make_unique<ErrorReporter>(_bus, _fans);
Matt Spinlere8122392020-09-24 13:22:18 -0500200 }
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600201}
202
Matthew Barth2d2caa32020-05-26 11:07:24 -0500203std::unique_ptr<RedundancyPolicy>
204 JsonConfig::getPolicy(const json& rpolicy, const fanPolicy& fpolicy)
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600205{
206 if (!rpolicy.contains("type"))
207 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500208 log<level::ERR>(
209 "Missing required fan presence policy type",
210 entry("FAN_NAME=%s",
211 std::get<fanPolicyFanPos>(std::get<Fan>(fpolicy)).c_str()),
212 entry("REQUIRED_PROPERTIES=%s", "{type}"));
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600213 throw std::runtime_error("Missing required fan presence policy type");
214 }
215
216 // The redundancy policy type for fan presence detection
217 // (Must have a supported function within the rpolicy namespace)
218 auto type = rpolicy["type"].get<std::string>();
219 std::transform(type.begin(), type.end(), type.begin(), tolower);
220 auto func = _rpolicies.find(type);
221 if (func != _rpolicies.end())
222 {
Matthew Barthf3e70472019-12-03 13:33:20 -0600223 // Call function for redundancy policy type and return the policy
224 return func->second(fpolicy);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600225 }
226 else
227 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500228 log<level::ERR>(
229 "Invalid fan presence policy type",
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600230 entry("FAN_NAME=%s",
Matthew Barth2d2caa32020-05-26 11:07:24 -0500231 std::get<fanPolicyFanPos>(std::get<Fan>(fpolicy)).c_str()),
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600232 entry("RPOLICY_TYPE=%s", type.c_str()));
233 throw std::runtime_error("Invalid fan presence methods policy type");
Matthew Barth4a94dec2019-11-15 10:40:47 -0600234 }
235}
236
Matthew Barthe7566632019-11-18 16:13:04 -0600237/**
238 * Methods of fan presence detection function definitions
239 */
240namespace method
241{
Matthew Barth2d2caa32020-05-26 11:07:24 -0500242// Get a constructed presence sensor for fan presence detection by tach
243std::unique_ptr<PresenceSensor> getTach(size_t fanIndex, const json& method)
244{
245 if (!method.contains("sensors") || method["sensors"].size() == 0)
Matthew Barthe7566632019-11-18 16:13:04 -0600246 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500247 log<level::ERR>("Missing required tach method properties",
248 entry("FAN_ENTRY=%d", fanIndex),
249 entry("REQUIRED_PROPERTIES=%s", "{sensors}"));
250 throw std::runtime_error("Missing required tach method properties");
Matthew Barthe7566632019-11-18 16:13:04 -0600251 }
252
Matthew Barth2d2caa32020-05-26 11:07:24 -0500253 std::vector<std::string> sensors;
254 for (auto& sensor : method["sensors"])
Matthew Barthe7566632019-11-18 16:13:04 -0600255 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500256 sensors.emplace_back(sensor.get<std::string>());
Matthew Barthe7566632019-11-18 16:13:04 -0600257 }
258
Matthew Barth2d2caa32020-05-26 11:07:24 -0500259 return std::make_unique<PolicyAccess<Tach, JsonConfig>>(fanIndex,
260 std::move(sensors));
261}
262
263// Get a constructed presence sensor for fan presence detection by gpio
264std::unique_ptr<PresenceSensor> getGpio(size_t fanIndex, const json& method)
265{
266 if (!method.contains("physpath") || !method.contains("devpath") ||
267 !method.contains("key"))
268 {
269 log<level::ERR>(
270 "Missing required gpio method properties",
271 entry("FAN_ENTRY=%d", fanIndex),
272 entry("REQUIRED_PROPERTIES=%s", "{physpath, devpath, key}"));
273 throw std::runtime_error("Missing required gpio method properties");
274 }
275
276 auto physpath = method["physpath"].get<std::string>();
277 auto devpath = method["devpath"].get<std::string>();
278 auto key = method["key"].get<unsigned int>();
279
Mike Cappsa35a8902021-06-10 10:20:14 -0400280 try
281 {
282 return std::make_unique<PolicyAccess<Gpio, JsonConfig>>(
283 fanIndex, physpath, devpath, key);
284 }
285 catch (const sdbusplus::exception_t& e)
286 {
287 namespace sdlogging = sdbusplus::xyz::openbmc_project::Logging::server;
288
289 log<level::ERR>(
290 fmt::format(
291 "Error creating Gpio device bridge, hardware not detected: {}",
292 e.what())
293 .c_str());
294
295 auto severity =
296 sdlogging::convertForMessage(sdlogging::Entry::Level::Error);
297
298 std::map<std::string, std::string> additionalData{
299 {"PHYSPATH", physpath},
300 {"DEVPATH", devpath},
301 {"FANINDEX", std::to_string(fanIndex)}};
302
303 try
304 {
305
306 util::SDBusPlus::lookupAndCallMethod(
307 loggingPath, loggingCreateIface, "Create",
308 "xyz.openbmc_project.Fan.Presence.Error.GPIODeviceUnavailable",
309 severity, additionalData);
310 }
311 catch (const util::DBusError& e)
312 {
313 log<level::ERR>(fmt::format("Call to create an error log for "
314 "presence-sensor failure failed: {}",
315 e.what())
316 .c_str());
317 }
318
319 return std::make_unique<PolicyAccess<NullGpio, JsonConfig>>();
320 }
Matthew Barth2d2caa32020-05-26 11:07:24 -0500321}
322
Matthew Barthe7566632019-11-18 16:13:04 -0600323} // namespace method
324
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600325/**
326 * Redundancy policies for fan presence detection function definitions
327 */
328namespace rpolicy
329{
Matthew Barth2d2caa32020-05-26 11:07:24 -0500330// Get an `Anyof` redundancy policy for the fan
331std::unique_ptr<RedundancyPolicy> getAnyof(const fanPolicy& fan)
332{
333 std::vector<std::reference_wrapper<PresenceSensor>> pSensors;
334 for (auto& fanSensor : std::get<fanPolicySensorListPos>(fan))
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600335 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500336 pSensors.emplace_back(*fanSensor);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600337 }
338
Matthew Barth2d2caa32020-05-26 11:07:24 -0500339 return std::make_unique<AnyOf>(std::get<fanPolicyFanPos>(fan), pSensors);
340}
Matthew Barth26ad44a2019-11-22 15:37:14 -0600341
Matthew Barth2d2caa32020-05-26 11:07:24 -0500342// Get a `Fallback` redundancy policy for the fan
343std::unique_ptr<RedundancyPolicy> getFallback(const fanPolicy& fan)
344{
345 std::vector<std::reference_wrapper<PresenceSensor>> pSensors;
346 for (auto& fanSensor : std::get<fanPolicySensorListPos>(fan))
347 {
348 // Place in the order given to fallback correctly
349 pSensors.emplace_back(*fanSensor);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600350 }
351
Matthew Barth2d2caa32020-05-26 11:07:24 -0500352 return std::make_unique<Fallback>(std::get<fanPolicyFanPos>(fan), pSensors);
353}
354
355} // namespace rpolicy
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600356
Matthew Barthfd05d642019-11-14 15:01:57 -0600357} // namespace presence
358} // namespace fan
359} // namespace phosphor