blob: c4ff3b856b99766fd790076a933ec7bd1a234372 [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)
Matt Spinler0daedd12021-01-25 14:46:03 -060051{}
Jolie Ku1a568652020-08-24 16:32:15 +080052
Matt Spinler0daedd12021-01-25 14:46:03 -060053void JsonConfig::start(const std::string& confFile)
54{
55 process(phosphor::fan::JsonConfig::load(confFile));
56
57 for (auto& p : _policies)
58 {
59 p->monitor();
60 }
Matthew Barthfd05d642019-11-14 15:01:57 -060061}
62
63const policies& JsonConfig::get()
64{
65 return _policies;
66}
67
Matthew Barthf3e70472019-12-03 13:33:20 -060068void JsonConfig::sighupHandler(sdeventplus::source::Signal& sigSrc,
69 const struct signalfd_siginfo* sigInfo)
70{
71 try
72 {
Jolie Ku1a568652020-08-24 16:32:15 +080073 using config = fan::JsonConfig;
74
Matt Spinler9e9f5992020-09-30 08:29:24 -050075 _reporter.reset();
76
Matthew Barthf3e70472019-12-03 13:33:20 -060077 // Load and process the json configuration
Jolie Ku1a568652020-08-24 16:32:15 +080078 process(
79 config::load(config::getConfFile(_bus, confAppName, confFileName)));
80
Matthew Barth2d2caa32020-05-26 11:07:24 -050081 for (auto& p : _policies)
Matthew Barthf3e70472019-12-03 13:33:20 -060082 {
83 p->monitor();
84 }
85 log<level::INFO>("Configuration loaded successfully");
86 }
87 catch (std::runtime_error& re)
88 {
89 log<level::ERR>("Error loading config, no config changes made",
90 entry("LOAD_ERROR=%s", re.what()));
91 }
92}
93
Matthew Barth4a94dec2019-11-15 10:40:47 -060094void JsonConfig::process(const json& jsonConf)
95{
Matthew Barthf3e70472019-12-03 13:33:20 -060096 policies policies;
97 std::vector<fanPolicy> fans;
Matthew Barthaa8d81d2019-11-21 14:07:31 -060098 // Set the expected number of fan entries
99 // to be size of the list of fan json config entries
100 // (Must be done to eliminate vector reallocation of fan references)
Matt Spinler9e9f5992020-09-30 08:29:24 -0500101 fans.reserve(jsonConf.size());
102 for (auto& member : jsonConf)
Matthew Barth4a94dec2019-11-15 10:40:47 -0600103 {
Matthew Barthe7566632019-11-18 16:13:04 -0600104 if (!member.contains("name") || !member.contains("path") ||
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600105 !member.contains("methods") || !member.contains("rpolicy"))
Matthew Barth4a94dec2019-11-15 10:40:47 -0600106 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500107 log<level::ERR>("Missing required fan presence properties",
108 entry("REQUIRED_PROPERTIES=%s",
109 "{name, path, methods, rpolicy}"));
Matthew Barth4a94dec2019-11-15 10:40:47 -0600110 throw std::runtime_error(
111 "Missing required fan presence properties");
112 }
Matthew Barthe7566632019-11-18 16:13:04 -0600113
114 // Loop thru the configured methods of presence detection
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600115 std::vector<std::unique_ptr<PresenceSensor>> sensors;
Matthew Barthe7566632019-11-18 16:13:04 -0600116 for (auto& method : member["methods"].items())
117 {
118 if (!method.value().contains("type"))
119 {
120 log<level::ERR>(
121 "Missing required fan presence method type",
122 entry("FAN_NAME=%s",
Matthew Barth2d2caa32020-05-26 11:07:24 -0500123 member["name"].get<std::string>().c_str()));
Matthew Barthe7566632019-11-18 16:13:04 -0600124 throw std::runtime_error(
125 "Missing required fan presence method type");
126 }
127 // The method type of fan presence detection
128 // (Must have a supported function within the method namespace)
129 auto type = method.value()["type"].get<std::string>();
130 std::transform(type.begin(), type.end(), type.begin(), tolower);
131 auto func = _methods.find(type);
132 if (func != _methods.end())
133 {
134 // Call function for method type
Matthew Barthf3e70472019-12-03 13:33:20 -0600135 auto sensor = func->second(fans.size(), method.value());
Matthew Barthe7566632019-11-18 16:13:04 -0600136 if (sensor)
137 {
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600138 sensors.emplace_back(std::move(sensor));
Matthew Barthe7566632019-11-18 16:13:04 -0600139 }
140 }
141 else
142 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500143 log<level::ERR>(
144 "Invalid fan presence method type",
145 entry("FAN_NAME=%s",
146 member["name"].get<std::string>().c_str()),
147 entry("METHOD_TYPE=%s", type.c_str()));
Matthew Barthe7566632019-11-18 16:13:04 -0600148 throw std::runtime_error("Invalid fan presence method type");
149 }
150 }
Matt Spinler9e9f5992020-09-30 08:29:24 -0500151
152 // Get the amount of time a fan must be not present before
153 // creating an error.
154 std::optional<size_t> timeUntilError;
155 if (member.contains("fan_missing_error_time"))
156 {
157 timeUntilError = member["fan_missing_error_time"].get<size_t>();
158 }
159
160 auto fan =
161 std::make_tuple(member["name"], member["path"], timeUntilError);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600162 // Create a fan object
Matthew Barthf3e70472019-12-03 13:33:20 -0600163 fans.emplace_back(std::make_tuple(fan, std::move(sensors)));
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600164
165 // Add fan presence policy
Matthew Barthf3e70472019-12-03 13:33:20 -0600166 auto policy = getPolicy(member["rpolicy"], fans.back());
167 if (policy)
168 {
169 policies.emplace_back(std::move(policy));
170 }
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600171 }
Matthew Barthf3e70472019-12-03 13:33:20 -0600172
173 // Success, refresh fans and policies lists
174 _fans.clear();
175 _fans.swap(fans);
176
177 _policies.clear();
178 _policies.swap(policies);
Matt Spinlere8122392020-09-24 13:22:18 -0500179
180 // Create the error reporter class if necessary
Matt Spinler9e9f5992020-09-30 08:29:24 -0500181 if (std::any_of(_fans.begin(), _fans.end(), [](const auto& fan) {
182 return std::get<std::optional<size_t>>(std::get<Fan>(fan)) !=
183 std::nullopt;
184 }))
Matt Spinlere8122392020-09-24 13:22:18 -0500185 {
Matt Spinler9e9f5992020-09-30 08:29:24 -0500186 _reporter = std::make_unique<ErrorReporter>(_bus, _fans);
Matt Spinlere8122392020-09-24 13:22:18 -0500187 }
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600188}
189
Matthew Barth2d2caa32020-05-26 11:07:24 -0500190std::unique_ptr<RedundancyPolicy>
191 JsonConfig::getPolicy(const json& rpolicy, const fanPolicy& fpolicy)
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600192{
193 if (!rpolicy.contains("type"))
194 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500195 log<level::ERR>(
196 "Missing required fan presence policy type",
197 entry("FAN_NAME=%s",
198 std::get<fanPolicyFanPos>(std::get<Fan>(fpolicy)).c_str()),
199 entry("REQUIRED_PROPERTIES=%s", "{type}"));
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600200 throw std::runtime_error("Missing required fan presence policy type");
201 }
202
203 // The redundancy policy type for fan presence detection
204 // (Must have a supported function within the rpolicy namespace)
205 auto type = rpolicy["type"].get<std::string>();
206 std::transform(type.begin(), type.end(), type.begin(), tolower);
207 auto func = _rpolicies.find(type);
208 if (func != _rpolicies.end())
209 {
Matthew Barthf3e70472019-12-03 13:33:20 -0600210 // Call function for redundancy policy type and return the policy
211 return func->second(fpolicy);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600212 }
213 else
214 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500215 log<level::ERR>(
216 "Invalid fan presence policy type",
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600217 entry("FAN_NAME=%s",
Matthew Barth2d2caa32020-05-26 11:07:24 -0500218 std::get<fanPolicyFanPos>(std::get<Fan>(fpolicy)).c_str()),
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600219 entry("RPOLICY_TYPE=%s", type.c_str()));
220 throw std::runtime_error("Invalid fan presence methods policy type");
Matthew Barth4a94dec2019-11-15 10:40:47 -0600221 }
222}
223
Matthew Barthe7566632019-11-18 16:13:04 -0600224/**
225 * Methods of fan presence detection function definitions
226 */
227namespace method
228{
Matthew Barth2d2caa32020-05-26 11:07:24 -0500229// Get a constructed presence sensor for fan presence detection by tach
230std::unique_ptr<PresenceSensor> getTach(size_t fanIndex, const json& method)
231{
232 if (!method.contains("sensors") || method["sensors"].size() == 0)
Matthew Barthe7566632019-11-18 16:13:04 -0600233 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500234 log<level::ERR>("Missing required tach method properties",
235 entry("FAN_ENTRY=%d", fanIndex),
236 entry("REQUIRED_PROPERTIES=%s", "{sensors}"));
237 throw std::runtime_error("Missing required tach method properties");
Matthew Barthe7566632019-11-18 16:13:04 -0600238 }
239
Matthew Barth2d2caa32020-05-26 11:07:24 -0500240 std::vector<std::string> sensors;
241 for (auto& sensor : method["sensors"])
Matthew Barthe7566632019-11-18 16:13:04 -0600242 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500243 sensors.emplace_back(sensor.get<std::string>());
Matthew Barthe7566632019-11-18 16:13:04 -0600244 }
245
Matthew Barth2d2caa32020-05-26 11:07:24 -0500246 return std::make_unique<PolicyAccess<Tach, JsonConfig>>(fanIndex,
247 std::move(sensors));
248}
249
250// Get a constructed presence sensor for fan presence detection by gpio
251std::unique_ptr<PresenceSensor> getGpio(size_t fanIndex, const json& method)
252{
253 if (!method.contains("physpath") || !method.contains("devpath") ||
254 !method.contains("key"))
255 {
256 log<level::ERR>(
257 "Missing required gpio method properties",
258 entry("FAN_ENTRY=%d", fanIndex),
259 entry("REQUIRED_PROPERTIES=%s", "{physpath, devpath, key}"));
260 throw std::runtime_error("Missing required gpio method properties");
261 }
262
263 auto physpath = method["physpath"].get<std::string>();
264 auto devpath = method["devpath"].get<std::string>();
265 auto key = method["key"].get<unsigned int>();
266
267 return std::make_unique<PolicyAccess<Gpio, JsonConfig>>(fanIndex, physpath,
268 devpath, key);
269}
270
Matthew Barthe7566632019-11-18 16:13:04 -0600271} // namespace method
272
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600273/**
274 * Redundancy policies for fan presence detection function definitions
275 */
276namespace rpolicy
277{
Matthew Barth2d2caa32020-05-26 11:07:24 -0500278// Get an `Anyof` redundancy policy for the fan
279std::unique_ptr<RedundancyPolicy> getAnyof(const fanPolicy& fan)
280{
281 std::vector<std::reference_wrapper<PresenceSensor>> pSensors;
282 for (auto& fanSensor : std::get<fanPolicySensorListPos>(fan))
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600283 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500284 pSensors.emplace_back(*fanSensor);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600285 }
286
Matthew Barth2d2caa32020-05-26 11:07:24 -0500287 return std::make_unique<AnyOf>(std::get<fanPolicyFanPos>(fan), pSensors);
288}
Matthew Barth26ad44a2019-11-22 15:37:14 -0600289
Matthew Barth2d2caa32020-05-26 11:07:24 -0500290// Get a `Fallback` redundancy policy for the fan
291std::unique_ptr<RedundancyPolicy> getFallback(const fanPolicy& fan)
292{
293 std::vector<std::reference_wrapper<PresenceSensor>> pSensors;
294 for (auto& fanSensor : std::get<fanPolicySensorListPos>(fan))
295 {
296 // Place in the order given to fallback correctly
297 pSensors.emplace_back(*fanSensor);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600298 }
299
Matthew Barth2d2caa32020-05-26 11:07:24 -0500300 return std::make_unique<Fallback>(std::get<fanPolicyFanPos>(fan), pSensors);
301}
302
303} // namespace rpolicy
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600304
Matthew Barthfd05d642019-11-14 15:01:57 -0600305} // namespace presence
306} // namespace fan
307} // namespace phosphor