blob: f9e1df6cc30e379af9f5fddcd3d0f42259715fb2 [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";
Matthew Barth90781312020-05-04 13:08:31 -050029constexpr auto confDbusPath = "/xyz/openbmc_project/inventory/system/chassis";
30constexpr auto confDbusIntf =
31 "xyz.openbmc_project.Inventory.Decorator.Compatible";
32constexpr auto confDbusProp = "Names";
Matthew Barthf6d7f612019-12-10 15:22:54 -060033
Matthew Barthfd05d642019-11-14 15:01:57 -060034using policies = std::vector<std::unique_ptr<RedundancyPolicy>>;
Matthew Barthaa8d81d2019-11-21 14:07:31 -060035
36constexpr auto fanPolicyFanPos = 0;
37constexpr auto fanPolicySensorListPos = 1;
38using fanPolicy = std::tuple<Fan, std::vector<std::unique_ptr<PresenceSensor>>>;
39
Matthew Barthe7566632019-11-18 16:13:04 -060040// Presence method handler function
Matthew Barth2d2caa32020-05-26 11:07:24 -050041using methodHandler =
42 std::function<std::unique_ptr<PresenceSensor>(size_t, const json&)>;
Matthew Barthaa8d81d2019-11-21 14:07:31 -060043// Presence redundancy policy handler function
Matthew Barth2d2caa32020-05-26 11:07:24 -050044using rpolicyHandler =
45 std::function<std::unique_ptr<RedundancyPolicy>(const fanPolicy&)>;
Matthew Barthfd05d642019-11-14 15:01:57 -060046
47class JsonConfig
48{
Matthew Barth2d2caa32020-05-26 11:07:24 -050049 public:
50 JsonConfig() = delete;
51 JsonConfig(const JsonConfig&) = delete;
52 JsonConfig(JsonConfig&&) = delete;
53 JsonConfig& operator=(const JsonConfig&) = delete;
54 JsonConfig& operator=(JsonConfig&&) = delete;
55 ~JsonConfig() = default;
Matthew Barthfd05d642019-11-14 15:01:57 -060056
Matthew Barth2d2caa32020-05-26 11:07:24 -050057 /**
58 * Constructor
59 * Parses and populates the fan presence policies from a json file
60 *
61 * @param[in] bus - sdbusplus bus object
62 */
63 explicit JsonConfig(sdbusplus::bus::bus& bus);
Matthew Barthfd05d642019-11-14 15:01:57 -060064
Matthew Barth2d2caa32020-05-26 11:07:24 -050065 /**
66 * @brief Get the json config based fan presence policies
67 *
68 * @return - The fan presence policies
69 */
70 static const policies& get();
Matthew Barthfd05d642019-11-14 15:01:57 -060071
Matthew Barth2d2caa32020-05-26 11:07:24 -050072 /**
73 * @brief Callback function to handle receiving a HUP signal to
74 * reload the json configuration.
75 *
76 * @param[in] sigSrc - sd_event_source signal wrapper
77 * @param[in] sigInfo - signal info on signal fd
78 */
79 void sighupHandler(sdeventplus::source::Signal& sigSrc,
80 const struct signalfd_siginfo* sigInfo);
Matthew Barthfd05d642019-11-14 15:01:57 -060081
Matthew Barth2d2caa32020-05-26 11:07:24 -050082 private:
83 /* Fan presence policies */
84 static policies _policies;
Matthew Barthf3e70472019-12-03 13:33:20 -060085
Matthew Barth2d2caa32020-05-26 11:07:24 -050086 /* The sdbusplus bus object */
87 sdbusplus::bus::bus& _bus;
Matthew Barthfd05d642019-11-14 15:01:57 -060088
Matthew Barth2d2caa32020-05-26 11:07:24 -050089 /* Config file to be used */
90 fs::path _confFile;
Matthew Barth4a94dec2019-11-15 10:40:47 -060091
Matthew Barth2d2caa32020-05-26 11:07:24 -050092 /* List of Fan objects to have presence policies */
93 std::vector<fanPolicy> _fans;
Matthew Barth5060b102019-12-16 10:46:35 -060094
Matthew Barth2d2caa32020-05-26 11:07:24 -050095 /* Presence methods mapping to their associated handler function */
96 static const std::map<std::string, methodHandler> _methods;
Matthew Barthf3e70472019-12-03 13:33:20 -060097
Matthew Barth2d2caa32020-05-26 11:07:24 -050098 /**
99 * Presence redundancy policy mapping to their associated handler
100 * function
101 */
102 static const std::map<std::string, rpolicyHandler> _rpolicies;
Matthew Barth4a94dec2019-11-15 10:40:47 -0600103
Matthew Barth2d2caa32020-05-26 11:07:24 -0500104 /**
105 * Get the json configuration file. The first location found to contain
106 * the json config file is used from the following locations in order.
Matthew Barth37b21102020-05-05 10:37:15 -0500107 * 1.) From the confOverridePath location
108 * 2.) From config file found using property value(s) as a relative
109 * path extension on the base path from the dbus object where:
110 * path = Path set in confDbusPath
Matthew Barth2d2caa32020-05-26 11:07:24 -0500111 * interface = Interface set in confDbusIntf
112 * property = Property set in confDbusProp
Matthew Barth37b21102020-05-05 10:37:15 -0500113 * 3.) *DEFAULT* - From the confBasePath location
Matthew Barth2d2caa32020-05-26 11:07:24 -0500114 */
115 const fs::path getConfFile();
Matthew Barthe7566632019-11-18 16:13:04 -0600116
Matthew Barth2d2caa32020-05-26 11:07:24 -0500117 /**
118 * @brief Load the json config file
119 */
120 void load();
Matthew Barthe7566632019-11-18 16:13:04 -0600121
Matthew Barth2d2caa32020-05-26 11:07:24 -0500122 /**
123 * @brief Process the json config to extract the defined fan presence
124 * policies.
125 *
126 * @param[in] jsonConf - parsed json configuration data
127 */
128 void process(const json& jsonConf);
Matthew Barth5060b102019-12-16 10:46:35 -0600129
Matthew Barth2d2caa32020-05-26 11:07:24 -0500130 /**
131 * @brief Get the redundancy policy of presence detection for a fan
132 *
133 * @param[in] rpolicy - policy type to construct
134 * @param[in] fpolicy - fan policy object
135 *
136 * @return - The constructed redundancy policy type for the fan
137 */
138 std::unique_ptr<RedundancyPolicy> getPolicy(const json& rpolicy,
139 const fanPolicy& fpolicy);
Matthew Barthfd05d642019-11-14 15:01:57 -0600140};
141
Matthew Barthe7566632019-11-18 16:13:04 -0600142/**
143 * Methods of fan presence detection function declarations
144 */
145namespace method
146{
Matthew Barth2d2caa32020-05-26 11:07:24 -0500147/**
148 * @brief Fan presence detection method by tach feedback
149 *
150 * @param[in] fanIndex - fan object index to add tach method
151 * @param[in] method - json properties for a tach method
152 *
153 * @return - A presence sensor to detect fan presence by tach feedback
154 */
155std::unique_ptr<PresenceSensor> getTach(size_t fanIndex, const json& method);
Matthew Barthe7566632019-11-18 16:13:04 -0600156
Matthew Barth2d2caa32020-05-26 11:07:24 -0500157/**
158 * @brief Fan presence detection method by gpio
159 *
160 * @param[in] fanIndex - fan object index to add gpio method
161 * @param[in] method - json properties for a gpio method
162 *
163 * @return - A presence sensor to detect fan presence by gpio
164 */
165std::unique_ptr<PresenceSensor> getGpio(size_t fanIndex, const json& method);
Matthew Barthe7566632019-11-18 16:13:04 -0600166
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{
Matthew Barth2d2caa32020-05-26 11:07:24 -0500174/**
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 */
182std::unique_ptr<RedundancyPolicy> getAnyof(const fanPolicy& fan);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600183
Matthew Barth2d2caa32020-05-26 11:07:24 -0500184/**
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 */
192std::unique_ptr<RedundancyPolicy> getFallback(const fanPolicy& fan);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600193
Matthew Barth2d2caa32020-05-26 11:07:24 -0500194} // namespace rpolicy
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600195
Matthew Barthfd05d642019-11-14 15:01:57 -0600196} // namespace presence
197} // namespace fan
198} // namespace phosphor