blob: 1e31939d2e52f5036ae5c160a3f4fea9b22b4426 [file] [log] [blame]
Matthew Barthfd05d642019-11-14 15:01:57 -06001#pragma once
2
3#include <string>
4#include <vector>
5#include <memory>
Matthew Barth4a94dec2019-11-15 10:40:47 -06006#include <nlohmann/json.hpp>
Matthew Barthf3e70472019-12-03 13:33:20 -06007#include <filesystem>
8#include <sdeventplus/source/signal.hpp>
Matthew Barth5060b102019-12-16 10:46:35 -06009#include <sdbusplus/bus.hpp>
Matthew Barthfd05d642019-11-14 15:01:57 -060010
11#include "config.h"
12#include "rpolicy.hpp"
Matthew Barth4a94dec2019-11-15 10:40:47 -060013#include "fan.hpp"
Matthew Barthe7566632019-11-18 16:13:04 -060014#include "psensor.hpp"
Matthew Barthfd05d642019-11-14 15:01:57 -060015
16namespace phosphor
17{
18namespace fan
19{
20namespace presence
21{
22
Matthew Barthf3e70472019-12-03 13:33:20 -060023namespace fs = std::filesystem;
Matthew Barth4a94dec2019-11-15 10:40:47 -060024using json = nlohmann::json;
Matthew Barthf6d7f612019-12-10 15:22:54 -060025
Matthew Barth5060b102019-12-16 10:46:35 -060026constexpr auto confFileName = "config.json";
27constexpr auto confOverridePath = "/etc/phosphor-fan-presence/presence";
28constexpr auto confBasePath = "/usr/share/phosphor-fan-presence/presence";
29constexpr auto confDbusIntf = "xyz.openbmc_project.Configs.ThermalApps";
30constexpr auto confDbusProp = "FanPresence";
Matthew Barthf6d7f612019-12-10 15:22:54 -060031
Matthew Barthfd05d642019-11-14 15:01:57 -060032using policies = std::vector<std::unique_ptr<RedundancyPolicy>>;
Matthew Barthaa8d81d2019-11-21 14:07:31 -060033
34constexpr auto fanPolicyFanPos = 0;
35constexpr auto fanPolicySensorListPos = 1;
36using fanPolicy = std::tuple<Fan, std::vector<std::unique_ptr<PresenceSensor>>>;
37
Matthew Barthe7566632019-11-18 16:13:04 -060038// Presence method handler function
39using methodHandler = std::function<
40 std::unique_ptr<PresenceSensor>(size_t, const json&)>;
Matthew Barthaa8d81d2019-11-21 14:07:31 -060041// Presence redundancy policy handler function
42using rpolicyHandler = std::function<
43 std::unique_ptr<RedundancyPolicy>(const fanPolicy&)>;
Matthew Barthfd05d642019-11-14 15:01:57 -060044
45class JsonConfig
46{
47 public:
48
49 JsonConfig() = delete;
50 JsonConfig(const JsonConfig&) = delete;
51 JsonConfig(JsonConfig&&) = delete;
52 JsonConfig& operator=(const JsonConfig&) = delete;
53 JsonConfig& operator=(JsonConfig&&) = delete;
54 ~JsonConfig() = default;
55
56 /**
57 * Constructor
58 * Parses and populates the fan presence policies from a json file
59 *
Matthew Barth5060b102019-12-16 10:46:35 -060060 * @param[in] bus - sdbusplus bus object
Matthew Barthfd05d642019-11-14 15:01:57 -060061 */
Matthew Barth5060b102019-12-16 10:46:35 -060062 explicit JsonConfig(sdbusplus::bus::bus& bus);
Matthew Barthfd05d642019-11-14 15:01:57 -060063
64 /**
65 * @brief Get the json config based fan presence policies
66 *
67 * @return - The fan presence policies
68 */
69 static const policies& get();
70
Matthew Barthf3e70472019-12-03 13:33:20 -060071 /**
72 * @brief Callback function to handle receiving a HUP signal to
73 * reload the json configuration.
74 *
75 * @param[in] sigSrc - sd_event_source signal wrapper
76 * @param[in] sigInfo - signal info on signal fd
77 */
78 void sighupHandler(sdeventplus::source::Signal& sigSrc,
79 const struct signalfd_siginfo* sigInfo);
80
Matthew Barthfd05d642019-11-14 15:01:57 -060081 private:
82
83 /* Fan presence policies */
84 static policies _policies;
Matthew Barth4a94dec2019-11-15 10:40:47 -060085
Matthew Barth5060b102019-12-16 10:46:35 -060086 /* The sdbusplus bus object */
87 sdbusplus::bus::bus& _bus;
88
89 /* Config file to be used */
90 fs::path _confFile;
Matthew Barthf3e70472019-12-03 13:33:20 -060091
Matthew Barth4a94dec2019-11-15 10:40:47 -060092 /* List of Fan objects to have presence policies */
Matthew Barthaa8d81d2019-11-21 14:07:31 -060093 std::vector<fanPolicy> _fans;
Matthew Barth4a94dec2019-11-15 10:40:47 -060094
Matthew Barthe7566632019-11-18 16:13:04 -060095 /* Presence methods mapping to their associated handler function */
96 static const std::map<std::string, methodHandler> _methods;
97
Matthew Barthaa8d81d2019-11-21 14:07:31 -060098 /**
99 * Presence redundancy policy mapping to their associated handler
100 * function
101 */
102 static const std::map<std::string, rpolicyHandler> _rpolicies;
Matthew Barthe7566632019-11-18 16:13:04 -0600103
Matthew Barth4a94dec2019-11-15 10:40:47 -0600104 /**
Matthew Barth5060b102019-12-16 10:46:35 -0600105 * Get the json configuration file. The first location found to contain
106 * the json config file is used from the following locations in order.
107 * 1.) confOverridePath ("/etc/phosphor-fan-presence/presence")
108 * 2.) confBasePath ("/usr/share/phosphor-fan-presence/presence")
109 * 3.) From first dbus object found hosting
110 * interface = Interface set in confDbusIntf
111 * property = Property set in confDbusProp
112 */
113 const fs::path getConfFile();
114
115 /**
Matthew Barthf3e70472019-12-03 13:33:20 -0600116 * @brief Load the json config file
117 */
118 void load();
119
120 /**
Matthew Barth4a94dec2019-11-15 10:40:47 -0600121 * @brief Process the json config to extract the defined fan presence
122 * policies.
123 *
124 * @param[in] jsonConf - parsed json configuration data
125 */
126 void process(const json& jsonConf);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600127
128 /**
Matthew Barthf3e70472019-12-03 13:33:20 -0600129 * @brief Get the redundancy policy of presence detection for a fan
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600130 *
Matthew Barthf3e70472019-12-03 13:33:20 -0600131 * @param[in] rpolicy - policy type to construct
132 * @param[in] fpolicy - fan policy object
133 *
134 * @return - The constructed redundancy policy type for the fan
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600135 */
Matthew Barthf3e70472019-12-03 13:33:20 -0600136 std::unique_ptr<RedundancyPolicy> getPolicy(const json& rpolicy,
137 const fanPolicy& fpolicy);
Matthew Barthfd05d642019-11-14 15:01:57 -0600138};
139
Matthew Barthe7566632019-11-18 16:13:04 -0600140/**
141 * Methods of fan presence detection function declarations
142 */
143namespace method
144{
145 /**
146 * @brief Fan presence detection method by tach feedback
147 *
148 * @param[in] fanIndex - fan object index to add tach method
149 * @param[in] method - json properties for a tach method
150 *
151 * @return - A presence sensor to detect fan presence by tach feedback
152 */
153 std::unique_ptr<PresenceSensor> getTach(size_t fanIndex,
154 const json& method);
155
156 /**
157 * @brief Fan presence detection method by gpio
158 *
159 * @param[in] fanIndex - fan object index to add gpio method
160 * @param[in] method - json properties for a gpio method
161 *
162 * @return - A presence sensor to detect fan presence by gpio
163 */
164 std::unique_ptr<PresenceSensor> getGpio(size_t fanIndex,
165 const json& method);
166
167} // namespace method
168
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600169/**
170 * Redundancy policies for fan presence detection function declarations
171 */
172namespace rpolicy
173{
174 /**
175 * @brief Create an `Anyof` redundancy policy on the created presence
176 * sensors for a fan
177 *
178 * @param[in] fan - fan policy object with the presence sensors for the fan
179 *
180 * @return - An `Anyof` redundancy policy
181 */
182 std::unique_ptr<RedundancyPolicy> getAnyof(const fanPolicy& fan);
183
184 /**
185 * @brief Create a `Fallback` redundancy policy on the created presence
186 * sensors for a fan
187 *
188 * @param[in] fan - fan policy object with the presence sensors for the fan
189 *
190 * @return - A `Fallback` redundancy policy
191 */
192 std::unique_ptr<RedundancyPolicy> getFallback(const fanPolicy& fan);
193
194} // namespace policy
195
Matthew Barthfd05d642019-11-14 15:01:57 -0600196} // namespace presence
197} // namespace fan
198} // namespace phosphor