blob: 6045e90d8767b212c6add9971a294438acef23a9 [file] [log] [blame]
Matthew Barthfd05d642019-11-14 15:01:57 -06001#pragma once
2
Matthew Barth4a94dec2019-11-15 10:40:47 -06003#include "fan.hpp"
Matthew Barthe7566632019-11-18 16:13:04 -06004#include "psensor.hpp"
Matthew Barth2d2caa32020-05-26 11:07:24 -05005#include "rpolicy.hpp"
6
7#include <nlohmann/json.hpp>
8#include <sdbusplus/bus.hpp>
9#include <sdeventplus/source/signal.hpp>
10
11#include <filesystem>
12#include <memory>
13#include <string>
14#include <vector>
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
Matthew Barth2d2caa32020-05-26 11:07:24 -050039using methodHandler =
40 std::function<std::unique_ptr<PresenceSensor>(size_t, const json&)>;
Matthew Barthaa8d81d2019-11-21 14:07:31 -060041// Presence redundancy policy handler function
Matthew Barth2d2caa32020-05-26 11:07:24 -050042using rpolicyHandler =
43 std::function<std::unique_ptr<RedundancyPolicy>(const fanPolicy&)>;
Matthew Barthfd05d642019-11-14 15:01:57 -060044
45class JsonConfig
46{
Matthew Barth2d2caa32020-05-26 11:07:24 -050047 public:
48 JsonConfig() = delete;
49 JsonConfig(const JsonConfig&) = delete;
50 JsonConfig(JsonConfig&&) = delete;
51 JsonConfig& operator=(const JsonConfig&) = delete;
52 JsonConfig& operator=(JsonConfig&&) = delete;
53 ~JsonConfig() = default;
Matthew Barthfd05d642019-11-14 15:01:57 -060054
Matthew Barth2d2caa32020-05-26 11:07:24 -050055 /**
56 * Constructor
57 * Parses and populates the fan presence policies from a json file
58 *
59 * @param[in] bus - sdbusplus bus object
60 */
61 explicit JsonConfig(sdbusplus::bus::bus& bus);
Matthew Barthfd05d642019-11-14 15:01:57 -060062
Matthew Barth2d2caa32020-05-26 11:07:24 -050063 /**
64 * @brief Get the json config based fan presence policies
65 *
66 * @return - The fan presence policies
67 */
68 static const policies& get();
Matthew Barthfd05d642019-11-14 15:01:57 -060069
Matthew Barth2d2caa32020-05-26 11:07:24 -050070 /**
71 * @brief Callback function to handle receiving a HUP signal to
72 * reload the json configuration.
73 *
74 * @param[in] sigSrc - sd_event_source signal wrapper
75 * @param[in] sigInfo - signal info on signal fd
76 */
77 void sighupHandler(sdeventplus::source::Signal& sigSrc,
78 const struct signalfd_siginfo* sigInfo);
Matthew Barthfd05d642019-11-14 15:01:57 -060079
Matthew Barth2d2caa32020-05-26 11:07:24 -050080 private:
81 /* Fan presence policies */
82 static policies _policies;
Matthew Barthf3e70472019-12-03 13:33:20 -060083
Matthew Barth2d2caa32020-05-26 11:07:24 -050084 /* The sdbusplus bus object */
85 sdbusplus::bus::bus& _bus;
Matthew Barthfd05d642019-11-14 15:01:57 -060086
Matthew Barth2d2caa32020-05-26 11:07:24 -050087 /* Config file to be used */
88 fs::path _confFile;
Matthew Barth4a94dec2019-11-15 10:40:47 -060089
Matthew Barth2d2caa32020-05-26 11:07:24 -050090 /* List of Fan objects to have presence policies */
91 std::vector<fanPolicy> _fans;
Matthew Barth5060b102019-12-16 10:46:35 -060092
Matthew Barth2d2caa32020-05-26 11:07:24 -050093 /* Presence methods mapping to their associated handler function */
94 static const std::map<std::string, methodHandler> _methods;
Matthew Barthf3e70472019-12-03 13:33:20 -060095
Matthew Barth2d2caa32020-05-26 11:07:24 -050096 /**
97 * Presence redundancy policy mapping to their associated handler
98 * function
99 */
100 static const std::map<std::string, rpolicyHandler> _rpolicies;
Matthew Barth4a94dec2019-11-15 10:40:47 -0600101
Matthew Barth2d2caa32020-05-26 11:07:24 -0500102 /**
103 * Get the json configuration file. The first location found to contain
104 * the json config file is used from the following locations in order.
105 * 1.) confOverridePath ("/etc/phosphor-fan-presence/presence")
106 * 2.) confBasePath ("/usr/share/phosphor-fan-presence/presence")
107 * 3.) From first dbus object found hosting
108 * interface = Interface set in confDbusIntf
109 * property = Property set in confDbusProp
110 */
111 const fs::path getConfFile();
Matthew Barthe7566632019-11-18 16:13:04 -0600112
Matthew Barth2d2caa32020-05-26 11:07:24 -0500113 /**
114 * @brief Load the json config file
115 */
116 void load();
Matthew Barthe7566632019-11-18 16:13:04 -0600117
Matthew Barth2d2caa32020-05-26 11:07:24 -0500118 /**
119 * @brief Process the json config to extract the defined fan presence
120 * policies.
121 *
122 * @param[in] jsonConf - parsed json configuration data
123 */
124 void process(const json& jsonConf);
Matthew Barth5060b102019-12-16 10:46:35 -0600125
Matthew Barth2d2caa32020-05-26 11:07:24 -0500126 /**
127 * @brief Get the redundancy policy of presence detection for a fan
128 *
129 * @param[in] rpolicy - policy type to construct
130 * @param[in] fpolicy - fan policy object
131 *
132 * @return - The constructed redundancy policy type for the fan
133 */
134 std::unique_ptr<RedundancyPolicy> getPolicy(const json& rpolicy,
135 const fanPolicy& fpolicy);
Matthew Barthfd05d642019-11-14 15:01:57 -0600136};
137
Matthew Barthe7566632019-11-18 16:13:04 -0600138/**
139 * Methods of fan presence detection function declarations
140 */
141namespace method
142{
Matthew Barth2d2caa32020-05-26 11:07:24 -0500143/**
144 * @brief Fan presence detection method by tach feedback
145 *
146 * @param[in] fanIndex - fan object index to add tach method
147 * @param[in] method - json properties for a tach method
148 *
149 * @return - A presence sensor to detect fan presence by tach feedback
150 */
151std::unique_ptr<PresenceSensor> getTach(size_t fanIndex, const json& method);
Matthew Barthe7566632019-11-18 16:13:04 -0600152
Matthew Barth2d2caa32020-05-26 11:07:24 -0500153/**
154 * @brief Fan presence detection method by gpio
155 *
156 * @param[in] fanIndex - fan object index to add gpio method
157 * @param[in] method - json properties for a gpio method
158 *
159 * @return - A presence sensor to detect fan presence by gpio
160 */
161std::unique_ptr<PresenceSensor> getGpio(size_t fanIndex, const json& method);
Matthew Barthe7566632019-11-18 16:13:04 -0600162
163} // namespace method
164
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600165/**
166 * Redundancy policies for fan presence detection function declarations
167 */
168namespace rpolicy
169{
Matthew Barth2d2caa32020-05-26 11:07:24 -0500170/**
171 * @brief Create an `Anyof` redundancy policy on the created presence
172 * sensors for a fan
173 *
174 * @param[in] fan - fan policy object with the presence sensors for the fan
175 *
176 * @return - An `Anyof` redundancy policy
177 */
178std::unique_ptr<RedundancyPolicy> getAnyof(const fanPolicy& fan);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600179
Matthew Barth2d2caa32020-05-26 11:07:24 -0500180/**
181 * @brief Create a `Fallback` redundancy policy on the created presence
182 * sensors for a fan
183 *
184 * @param[in] fan - fan policy object with the presence sensors for the fan
185 *
186 * @return - A `Fallback` redundancy policy
187 */
188std::unique_ptr<RedundancyPolicy> getFallback(const fanPolicy& fan);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600189
Matthew Barth2d2caa32020-05-26 11:07:24 -0500190} // namespace rpolicy
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600191
Matthew Barthfd05d642019-11-14 15:01:57 -0600192} // namespace presence
193} // namespace fan
194} // namespace phosphor