blob: d850a950a97b81e72de2029e77b05aac73436d9d [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>
Matthew Barthfd05d642019-11-14 15:01:57 -060028
Matthew Barth2d2caa32020-05-26 11:07:24 -050029#include <filesystem>
30#include <fstream>
31#include <string>
Matthew Barthfd05d642019-11-14 15:01:57 -060032
33namespace phosphor
34{
35namespace fan
36{
37namespace presence
38{
39
Matthew Barth0961fae2019-11-15 09:00:27 -060040using json = nlohmann::json;
41namespace fs = std::filesystem;
42using namespace phosphor::logging;
43
Matthew Barthfd05d642019-11-14 15:01:57 -060044policies JsonConfig::_policies;
Matthew Barth2d2caa32020-05-26 11:07:24 -050045const std::map<std::string, methodHandler> JsonConfig::_methods = {
46 {"tach", method::getTach}, {"gpio", method::getGpio}};
47const std::map<std::string, rpolicyHandler> JsonConfig::_rpolicies = {
48 {"anyof", rpolicy::getAnyof}, {"fallback", rpolicy::getFallback}};
Matthew Barthfd05d642019-11-14 15:01:57 -060049
Matthew Barth2d2caa32020-05-26 11:07:24 -050050JsonConfig::JsonConfig(sdbusplus::bus::bus& bus) : _bus(bus)
Matthew Barthfd05d642019-11-14 15:01:57 -060051{
Jolie Ku1a568652020-08-24 16:32:15 +080052 using config = fan::JsonConfig;
53
Matthew Barth5060b102019-12-16 10:46:35 -060054 // Load and process the json configuration
Jolie Ku1a568652020-08-24 16:32:15 +080055 process(config::load(config::getConfFile(bus, confAppName, confFileName)));
Matthew Barthfd05d642019-11-14 15:01:57 -060056}
57
58const policies& JsonConfig::get()
59{
60 return _policies;
61}
62
Matthew Barthf3e70472019-12-03 13:33:20 -060063void JsonConfig::sighupHandler(sdeventplus::source::Signal& sigSrc,
64 const struct signalfd_siginfo* sigInfo)
65{
66 try
67 {
Jolie Ku1a568652020-08-24 16:32:15 +080068 using config = fan::JsonConfig;
69
Matt Spinler9e9f5992020-09-30 08:29:24 -050070 _reporter.reset();
71
Matthew Barthf3e70472019-12-03 13:33:20 -060072 // Load and process the json configuration
Jolie Ku1a568652020-08-24 16:32:15 +080073 process(
74 config::load(config::getConfFile(_bus, confAppName, confFileName)));
75
Matthew Barth2d2caa32020-05-26 11:07:24 -050076 for (auto& p : _policies)
Matthew Barthf3e70472019-12-03 13:33:20 -060077 {
78 p->monitor();
79 }
80 log<level::INFO>("Configuration loaded successfully");
81 }
82 catch (std::runtime_error& re)
83 {
84 log<level::ERR>("Error loading config, no config changes made",
85 entry("LOAD_ERROR=%s", re.what()));
86 }
87}
88
Matthew Barth4a94dec2019-11-15 10:40:47 -060089void JsonConfig::process(const json& jsonConf)
90{
Matthew Barthf3e70472019-12-03 13:33:20 -060091 policies policies;
92 std::vector<fanPolicy> fans;
Matthew Barthaa8d81d2019-11-21 14:07:31 -060093 // Set the expected number of fan entries
94 // to be size of the list of fan json config entries
95 // (Must be done to eliminate vector reallocation of fan references)
Matt Spinler9e9f5992020-09-30 08:29:24 -050096 fans.reserve(jsonConf.size());
97 for (auto& member : jsonConf)
Matthew Barth4a94dec2019-11-15 10:40:47 -060098 {
Matthew Barthe7566632019-11-18 16:13:04 -060099 if (!member.contains("name") || !member.contains("path") ||
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600100 !member.contains("methods") || !member.contains("rpolicy"))
Matthew Barth4a94dec2019-11-15 10:40:47 -0600101 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500102 log<level::ERR>("Missing required fan presence properties",
103 entry("REQUIRED_PROPERTIES=%s",
104 "{name, path, methods, rpolicy}"));
Matthew Barth4a94dec2019-11-15 10:40:47 -0600105 throw std::runtime_error(
106 "Missing required fan presence properties");
107 }
Matthew Barthe7566632019-11-18 16:13:04 -0600108
109 // Loop thru the configured methods of presence detection
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600110 std::vector<std::unique_ptr<PresenceSensor>> sensors;
Matthew Barthe7566632019-11-18 16:13:04 -0600111 for (auto& method : member["methods"].items())
112 {
113 if (!method.value().contains("type"))
114 {
115 log<level::ERR>(
116 "Missing required fan presence method type",
117 entry("FAN_NAME=%s",
Matthew Barth2d2caa32020-05-26 11:07:24 -0500118 member["name"].get<std::string>().c_str()));
Matthew Barthe7566632019-11-18 16:13:04 -0600119 throw std::runtime_error(
120 "Missing required fan presence method type");
121 }
122 // The method type of fan presence detection
123 // (Must have a supported function within the method namespace)
124 auto type = method.value()["type"].get<std::string>();
125 std::transform(type.begin(), type.end(), type.begin(), tolower);
126 auto func = _methods.find(type);
127 if (func != _methods.end())
128 {
129 // Call function for method type
Matthew Barthf3e70472019-12-03 13:33:20 -0600130 auto sensor = func->second(fans.size(), method.value());
Matthew Barthe7566632019-11-18 16:13:04 -0600131 if (sensor)
132 {
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600133 sensors.emplace_back(std::move(sensor));
Matthew Barthe7566632019-11-18 16:13:04 -0600134 }
135 }
136 else
137 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500138 log<level::ERR>(
139 "Invalid fan presence method type",
140 entry("FAN_NAME=%s",
141 member["name"].get<std::string>().c_str()),
142 entry("METHOD_TYPE=%s", type.c_str()));
Matthew Barthe7566632019-11-18 16:13:04 -0600143 throw std::runtime_error("Invalid fan presence method type");
144 }
145 }
Matt Spinler9e9f5992020-09-30 08:29:24 -0500146
147 // Get the amount of time a fan must be not present before
148 // creating an error.
149 std::optional<size_t> timeUntilError;
150 if (member.contains("fan_missing_error_time"))
151 {
152 timeUntilError = member["fan_missing_error_time"].get<size_t>();
153 }
154
155 auto fan =
156 std::make_tuple(member["name"], member["path"], timeUntilError);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600157 // Create a fan object
Matthew Barthf3e70472019-12-03 13:33:20 -0600158 fans.emplace_back(std::make_tuple(fan, std::move(sensors)));
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600159
160 // Add fan presence policy
Matthew Barthf3e70472019-12-03 13:33:20 -0600161 auto policy = getPolicy(member["rpolicy"], fans.back());
162 if (policy)
163 {
164 policies.emplace_back(std::move(policy));
165 }
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600166 }
Matthew Barthf3e70472019-12-03 13:33:20 -0600167
168 // Success, refresh fans and policies lists
169 _fans.clear();
170 _fans.swap(fans);
171
172 _policies.clear();
173 _policies.swap(policies);
Matt Spinlere8122392020-09-24 13:22:18 -0500174
175 // Create the error reporter class if necessary
Matt Spinler9e9f5992020-09-30 08:29:24 -0500176 if (std::any_of(_fans.begin(), _fans.end(), [](const auto& fan) {
177 return std::get<std::optional<size_t>>(std::get<Fan>(fan)) !=
178 std::nullopt;
179 }))
Matt Spinlere8122392020-09-24 13:22:18 -0500180 {
Matt Spinler9e9f5992020-09-30 08:29:24 -0500181 _reporter = std::make_unique<ErrorReporter>(_bus, _fans);
Matt Spinlere8122392020-09-24 13:22:18 -0500182 }
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600183}
184
Matthew Barth2d2caa32020-05-26 11:07:24 -0500185std::unique_ptr<RedundancyPolicy>
186 JsonConfig::getPolicy(const json& rpolicy, const fanPolicy& fpolicy)
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600187{
188 if (!rpolicy.contains("type"))
189 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500190 log<level::ERR>(
191 "Missing required fan presence policy type",
192 entry("FAN_NAME=%s",
193 std::get<fanPolicyFanPos>(std::get<Fan>(fpolicy)).c_str()),
194 entry("REQUIRED_PROPERTIES=%s", "{type}"));
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600195 throw std::runtime_error("Missing required fan presence policy type");
196 }
197
198 // The redundancy policy type for fan presence detection
199 // (Must have a supported function within the rpolicy namespace)
200 auto type = rpolicy["type"].get<std::string>();
201 std::transform(type.begin(), type.end(), type.begin(), tolower);
202 auto func = _rpolicies.find(type);
203 if (func != _rpolicies.end())
204 {
Matthew Barthf3e70472019-12-03 13:33:20 -0600205 // Call function for redundancy policy type and return the policy
206 return func->second(fpolicy);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600207 }
208 else
209 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500210 log<level::ERR>(
211 "Invalid fan presence policy type",
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600212 entry("FAN_NAME=%s",
Matthew Barth2d2caa32020-05-26 11:07:24 -0500213 std::get<fanPolicyFanPos>(std::get<Fan>(fpolicy)).c_str()),
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600214 entry("RPOLICY_TYPE=%s", type.c_str()));
215 throw std::runtime_error("Invalid fan presence methods policy type");
Matthew Barth4a94dec2019-11-15 10:40:47 -0600216 }
217}
218
Matthew Barthe7566632019-11-18 16:13:04 -0600219/**
220 * Methods of fan presence detection function definitions
221 */
222namespace method
223{
Matthew Barth2d2caa32020-05-26 11:07:24 -0500224// Get a constructed presence sensor for fan presence detection by tach
225std::unique_ptr<PresenceSensor> getTach(size_t fanIndex, const json& method)
226{
227 if (!method.contains("sensors") || method["sensors"].size() == 0)
Matthew Barthe7566632019-11-18 16:13:04 -0600228 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500229 log<level::ERR>("Missing required tach method properties",
230 entry("FAN_ENTRY=%d", fanIndex),
231 entry("REQUIRED_PROPERTIES=%s", "{sensors}"));
232 throw std::runtime_error("Missing required tach method properties");
Matthew Barthe7566632019-11-18 16:13:04 -0600233 }
234
Matthew Barth2d2caa32020-05-26 11:07:24 -0500235 std::vector<std::string> sensors;
236 for (auto& sensor : method["sensors"])
Matthew Barthe7566632019-11-18 16:13:04 -0600237 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500238 sensors.emplace_back(sensor.get<std::string>());
Matthew Barthe7566632019-11-18 16:13:04 -0600239 }
240
Matthew Barth2d2caa32020-05-26 11:07:24 -0500241 return std::make_unique<PolicyAccess<Tach, JsonConfig>>(fanIndex,
242 std::move(sensors));
243}
244
245// Get a constructed presence sensor for fan presence detection by gpio
246std::unique_ptr<PresenceSensor> getGpio(size_t fanIndex, const json& method)
247{
248 if (!method.contains("physpath") || !method.contains("devpath") ||
249 !method.contains("key"))
250 {
251 log<level::ERR>(
252 "Missing required gpio method properties",
253 entry("FAN_ENTRY=%d", fanIndex),
254 entry("REQUIRED_PROPERTIES=%s", "{physpath, devpath, key}"));
255 throw std::runtime_error("Missing required gpio method properties");
256 }
257
258 auto physpath = method["physpath"].get<std::string>();
259 auto devpath = method["devpath"].get<std::string>();
260 auto key = method["key"].get<unsigned int>();
261
262 return std::make_unique<PolicyAccess<Gpio, JsonConfig>>(fanIndex, physpath,
263 devpath, key);
264}
265
Matthew Barthe7566632019-11-18 16:13:04 -0600266} // namespace method
267
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600268/**
269 * Redundancy policies for fan presence detection function definitions
270 */
271namespace rpolicy
272{
Matthew Barth2d2caa32020-05-26 11:07:24 -0500273// Get an `Anyof` redundancy policy for the fan
274std::unique_ptr<RedundancyPolicy> getAnyof(const fanPolicy& fan)
275{
276 std::vector<std::reference_wrapper<PresenceSensor>> pSensors;
277 for (auto& fanSensor : std::get<fanPolicySensorListPos>(fan))
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600278 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500279 pSensors.emplace_back(*fanSensor);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600280 }
281
Matthew Barth2d2caa32020-05-26 11:07:24 -0500282 return std::make_unique<AnyOf>(std::get<fanPolicyFanPos>(fan), pSensors);
283}
Matthew Barth26ad44a2019-11-22 15:37:14 -0600284
Matthew Barth2d2caa32020-05-26 11:07:24 -0500285// Get a `Fallback` redundancy policy for the fan
286std::unique_ptr<RedundancyPolicy> getFallback(const fanPolicy& fan)
287{
288 std::vector<std::reference_wrapper<PresenceSensor>> pSensors;
289 for (auto& fanSensor : std::get<fanPolicySensorListPos>(fan))
290 {
291 // Place in the order given to fallback correctly
292 pSensors.emplace_back(*fanSensor);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600293 }
294
Matthew Barth2d2caa32020-05-26 11:07:24 -0500295 return std::make_unique<Fallback>(std::get<fanPolicyFanPos>(fan), pSensors);
296}
297
298} // namespace rpolicy
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600299
Matthew Barthfd05d642019-11-14 15:01:57 -0600300} // namespace presence
301} // namespace fan
302} // namespace phosphor