blob: 83267a763c391e648475b4e460987eab99753165 [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
Matthew Barthf3e70472019-12-03 13:33:20 -060070 // Load and process the json configuration
Jolie Ku1a568652020-08-24 16:32:15 +080071 process(
72 config::load(config::getConfFile(_bus, confAppName, confFileName)));
73
Matthew Barth2d2caa32020-05-26 11:07:24 -050074 for (auto& p : _policies)
Matthew Barthf3e70472019-12-03 13:33:20 -060075 {
76 p->monitor();
77 }
78 log<level::INFO>("Configuration loaded successfully");
79 }
80 catch (std::runtime_error& re)
81 {
82 log<level::ERR>("Error loading config, no config changes made",
83 entry("LOAD_ERROR=%s", re.what()));
84 }
85}
86
Matthew Barth4a94dec2019-11-15 10:40:47 -060087void JsonConfig::process(const json& jsonConf)
88{
Matthew Barthf3e70472019-12-03 13:33:20 -060089 policies policies;
90 std::vector<fanPolicy> fans;
Matthew Barthaa8d81d2019-11-21 14:07:31 -060091 // Set the expected number of fan entries
92 // to be size of the list of fan json config entries
93 // (Must be done to eliminate vector reallocation of fan references)
Matthew Barthf3e70472019-12-03 13:33:20 -060094 fans.reserve(jsonConf.size());
Matthew Barth4a94dec2019-11-15 10:40:47 -060095 for (auto& member : jsonConf)
96 {
Matthew Barthe7566632019-11-18 16:13:04 -060097 if (!member.contains("name") || !member.contains("path") ||
Matthew Barthaa8d81d2019-11-21 14:07:31 -060098 !member.contains("methods") || !member.contains("rpolicy"))
Matthew Barth4a94dec2019-11-15 10:40:47 -060099 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500100 log<level::ERR>("Missing required fan presence properties",
101 entry("REQUIRED_PROPERTIES=%s",
102 "{name, path, methods, rpolicy}"));
Matthew Barth4a94dec2019-11-15 10:40:47 -0600103 throw std::runtime_error(
104 "Missing required fan presence properties");
105 }
Matthew Barthe7566632019-11-18 16:13:04 -0600106
107 // Loop thru the configured methods of presence detection
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600108 std::vector<std::unique_ptr<PresenceSensor>> sensors;
Matthew Barthe7566632019-11-18 16:13:04 -0600109 for (auto& method : member["methods"].items())
110 {
111 if (!method.value().contains("type"))
112 {
113 log<level::ERR>(
114 "Missing required fan presence method type",
115 entry("FAN_NAME=%s",
Matthew Barth2d2caa32020-05-26 11:07:24 -0500116 member["name"].get<std::string>().c_str()));
Matthew Barthe7566632019-11-18 16:13:04 -0600117 throw std::runtime_error(
118 "Missing required fan presence method type");
119 }
120 // The method type of fan presence detection
121 // (Must have a supported function within the method namespace)
122 auto type = method.value()["type"].get<std::string>();
123 std::transform(type.begin(), type.end(), type.begin(), tolower);
124 auto func = _methods.find(type);
125 if (func != _methods.end())
126 {
127 // Call function for method type
Matthew Barthf3e70472019-12-03 13:33:20 -0600128 auto sensor = func->second(fans.size(), method.value());
Matthew Barthe7566632019-11-18 16:13:04 -0600129 if (sensor)
130 {
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600131 sensors.emplace_back(std::move(sensor));
Matthew Barthe7566632019-11-18 16:13:04 -0600132 }
133 }
134 else
135 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500136 log<level::ERR>(
137 "Invalid fan presence method type",
138 entry("FAN_NAME=%s",
139 member["name"].get<std::string>().c_str()),
140 entry("METHOD_TYPE=%s", type.c_str()));
Matthew Barthe7566632019-11-18 16:13:04 -0600141 throw std::runtime_error("Invalid fan presence method type");
142 }
143 }
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600144 auto fan = std::make_tuple(member["name"], member["path"]);
145 // Create a fan object
Matthew Barthf3e70472019-12-03 13:33:20 -0600146 fans.emplace_back(std::make_tuple(fan, std::move(sensors)));
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600147
148 // Add fan presence policy
Matthew Barthf3e70472019-12-03 13:33:20 -0600149 auto policy = getPolicy(member["rpolicy"], fans.back());
150 if (policy)
151 {
152 policies.emplace_back(std::move(policy));
153 }
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600154 }
Matthew Barthf3e70472019-12-03 13:33:20 -0600155
156 // Success, refresh fans and policies lists
157 _fans.clear();
158 _fans.swap(fans);
159
160 _policies.clear();
161 _policies.swap(policies);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600162}
163
Matthew Barth2d2caa32020-05-26 11:07:24 -0500164std::unique_ptr<RedundancyPolicy>
165 JsonConfig::getPolicy(const json& rpolicy, const fanPolicy& fpolicy)
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600166{
167 if (!rpolicy.contains("type"))
168 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500169 log<level::ERR>(
170 "Missing required fan presence policy type",
171 entry("FAN_NAME=%s",
172 std::get<fanPolicyFanPos>(std::get<Fan>(fpolicy)).c_str()),
173 entry("REQUIRED_PROPERTIES=%s", "{type}"));
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600174 throw std::runtime_error("Missing required fan presence policy type");
175 }
176
177 // The redundancy policy type for fan presence detection
178 // (Must have a supported function within the rpolicy namespace)
179 auto type = rpolicy["type"].get<std::string>();
180 std::transform(type.begin(), type.end(), type.begin(), tolower);
181 auto func = _rpolicies.find(type);
182 if (func != _rpolicies.end())
183 {
Matthew Barthf3e70472019-12-03 13:33:20 -0600184 // Call function for redundancy policy type and return the policy
185 return func->second(fpolicy);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600186 }
187 else
188 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500189 log<level::ERR>(
190 "Invalid fan presence policy type",
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600191 entry("FAN_NAME=%s",
Matthew Barth2d2caa32020-05-26 11:07:24 -0500192 std::get<fanPolicyFanPos>(std::get<Fan>(fpolicy)).c_str()),
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600193 entry("RPOLICY_TYPE=%s", type.c_str()));
194 throw std::runtime_error("Invalid fan presence methods policy type");
Matthew Barth4a94dec2019-11-15 10:40:47 -0600195 }
196}
197
Matthew Barthe7566632019-11-18 16:13:04 -0600198/**
199 * Methods of fan presence detection function definitions
200 */
201namespace method
202{
Matthew Barth2d2caa32020-05-26 11:07:24 -0500203// Get a constructed presence sensor for fan presence detection by tach
204std::unique_ptr<PresenceSensor> getTach(size_t fanIndex, const json& method)
205{
206 if (!method.contains("sensors") || method["sensors"].size() == 0)
Matthew Barthe7566632019-11-18 16:13:04 -0600207 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500208 log<level::ERR>("Missing required tach method properties",
209 entry("FAN_ENTRY=%d", fanIndex),
210 entry("REQUIRED_PROPERTIES=%s", "{sensors}"));
211 throw std::runtime_error("Missing required tach method properties");
Matthew Barthe7566632019-11-18 16:13:04 -0600212 }
213
Matthew Barth2d2caa32020-05-26 11:07:24 -0500214 std::vector<std::string> sensors;
215 for (auto& sensor : method["sensors"])
Matthew Barthe7566632019-11-18 16:13:04 -0600216 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500217 sensors.emplace_back(sensor.get<std::string>());
Matthew Barthe7566632019-11-18 16:13:04 -0600218 }
219
Matthew Barth2d2caa32020-05-26 11:07:24 -0500220 return std::make_unique<PolicyAccess<Tach, JsonConfig>>(fanIndex,
221 std::move(sensors));
222}
223
224// Get a constructed presence sensor for fan presence detection by gpio
225std::unique_ptr<PresenceSensor> getGpio(size_t fanIndex, const json& method)
226{
227 if (!method.contains("physpath") || !method.contains("devpath") ||
228 !method.contains("key"))
229 {
230 log<level::ERR>(
231 "Missing required gpio method properties",
232 entry("FAN_ENTRY=%d", fanIndex),
233 entry("REQUIRED_PROPERTIES=%s", "{physpath, devpath, key}"));
234 throw std::runtime_error("Missing required gpio method properties");
235 }
236
237 auto physpath = method["physpath"].get<std::string>();
238 auto devpath = method["devpath"].get<std::string>();
239 auto key = method["key"].get<unsigned int>();
240
241 return std::make_unique<PolicyAccess<Gpio, JsonConfig>>(fanIndex, physpath,
242 devpath, key);
243}
244
Matthew Barthe7566632019-11-18 16:13:04 -0600245} // namespace method
246
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600247/**
248 * Redundancy policies for fan presence detection function definitions
249 */
250namespace rpolicy
251{
Matthew Barth2d2caa32020-05-26 11:07:24 -0500252// Get an `Anyof` redundancy policy for the fan
253std::unique_ptr<RedundancyPolicy> getAnyof(const fanPolicy& fan)
254{
255 std::vector<std::reference_wrapper<PresenceSensor>> pSensors;
256 for (auto& fanSensor : std::get<fanPolicySensorListPos>(fan))
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600257 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500258 pSensors.emplace_back(*fanSensor);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600259 }
260
Matthew Barth2d2caa32020-05-26 11:07:24 -0500261 return std::make_unique<AnyOf>(std::get<fanPolicyFanPos>(fan), pSensors);
262}
Matthew Barth26ad44a2019-11-22 15:37:14 -0600263
Matthew Barth2d2caa32020-05-26 11:07:24 -0500264// Get a `Fallback` redundancy policy for the fan
265std::unique_ptr<RedundancyPolicy> getFallback(const fanPolicy& fan)
266{
267 std::vector<std::reference_wrapper<PresenceSensor>> pSensors;
268 for (auto& fanSensor : std::get<fanPolicySensorListPos>(fan))
269 {
270 // Place in the order given to fallback correctly
271 pSensors.emplace_back(*fanSensor);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600272 }
273
Matthew Barth2d2caa32020-05-26 11:07:24 -0500274 return std::make_unique<Fallback>(std::get<fanPolicyFanPos>(fan), pSensors);
275}
276
277} // namespace rpolicy
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600278
Matthew Barthfd05d642019-11-14 15:01:57 -0600279} // namespace presence
280} // namespace fan
281} // namespace phosphor