blob: 7a3f7b8b7a65e09f21e99f486d054ff0736bb125 [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
Matthew Barthfd05d642019-11-14 15:01:57 -060011#include "rpolicy.hpp"
Matthew Barth4a94dec2019-11-15 10:40:47 -060012#include "fan.hpp"
Matthew Barthe7566632019-11-18 16:13:04 -060013#include "psensor.hpp"
Matthew Barthfd05d642019-11-14 15:01:57 -060014
15namespace phosphor
16{
17namespace fan
18{
19namespace presence
20{
21
Matthew Barthf3e70472019-12-03 13:33:20 -060022namespace fs = std::filesystem;
Matthew Barth4a94dec2019-11-15 10:40:47 -060023using json = nlohmann::json;
Matthew Barthf6d7f612019-12-10 15:22:54 -060024
Matthew Barth5060b102019-12-16 10:46:35 -060025constexpr auto confFileName = "config.json";
26constexpr auto confOverridePath = "/etc/phosphor-fan-presence/presence";
27constexpr auto confBasePath = "/usr/share/phosphor-fan-presence/presence";
28constexpr auto confDbusIntf = "xyz.openbmc_project.Configs.ThermalApps";
29constexpr auto confDbusProp = "FanPresence";
Matthew Barthf6d7f612019-12-10 15:22:54 -060030
Matthew Barthfd05d642019-11-14 15:01:57 -060031using policies = std::vector<std::unique_ptr<RedundancyPolicy>>;
Matthew Barthaa8d81d2019-11-21 14:07:31 -060032
33constexpr auto fanPolicyFanPos = 0;
34constexpr auto fanPolicySensorListPos = 1;
35using fanPolicy = std::tuple<Fan, std::vector<std::unique_ptr<PresenceSensor>>>;
36
Matthew Barthe7566632019-11-18 16:13:04 -060037// Presence method handler function
38using methodHandler = std::function<
39 std::unique_ptr<PresenceSensor>(size_t, const json&)>;
Matthew Barthaa8d81d2019-11-21 14:07:31 -060040// Presence redundancy policy handler function
41using rpolicyHandler = std::function<
42 std::unique_ptr<RedundancyPolicy>(const fanPolicy&)>;
Matthew Barthfd05d642019-11-14 15:01:57 -060043
44class JsonConfig
45{
46 public:
47
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;
54
55 /**
56 * Constructor
57 * Parses and populates the fan presence policies from a json file
58 *
Matthew Barth5060b102019-12-16 10:46:35 -060059 * @param[in] bus - sdbusplus bus object
Matthew Barthfd05d642019-11-14 15:01:57 -060060 */
Matthew Barth5060b102019-12-16 10:46:35 -060061 explicit JsonConfig(sdbusplus::bus::bus& bus);
Matthew Barthfd05d642019-11-14 15:01:57 -060062
63 /**
64 * @brief Get the json config based fan presence policies
65 *
66 * @return - The fan presence policies
67 */
68 static const policies& get();
69
Matthew Barthf3e70472019-12-03 13:33:20 -060070 /**
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);
79
Matthew Barthfd05d642019-11-14 15:01:57 -060080 private:
81
82 /* Fan presence policies */
83 static policies _policies;
Matthew Barth4a94dec2019-11-15 10:40:47 -060084
Matthew Barth5060b102019-12-16 10:46:35 -060085 /* The sdbusplus bus object */
86 sdbusplus::bus::bus& _bus;
87
88 /* Config file to be used */
89 fs::path _confFile;
Matthew Barthf3e70472019-12-03 13:33:20 -060090
Matthew Barth4a94dec2019-11-15 10:40:47 -060091 /* List of Fan objects to have presence policies */
Matthew Barthaa8d81d2019-11-21 14:07:31 -060092 std::vector<fanPolicy> _fans;
Matthew Barth4a94dec2019-11-15 10:40:47 -060093
Matthew Barthe7566632019-11-18 16:13:04 -060094 /* Presence methods mapping to their associated handler function */
95 static const std::map<std::string, methodHandler> _methods;
96
Matthew Barthaa8d81d2019-11-21 14:07:31 -060097 /**
98 * Presence redundancy policy mapping to their associated handler
99 * function
100 */
101 static const std::map<std::string, rpolicyHandler> _rpolicies;
Matthew Barthe7566632019-11-18 16:13:04 -0600102
Matthew Barth4a94dec2019-11-15 10:40:47 -0600103 /**
Matthew Barth5060b102019-12-16 10:46:35 -0600104 * Get the json configuration file. The first location found to contain
105 * the json config file is used from the following locations in order.
106 * 1.) confOverridePath ("/etc/phosphor-fan-presence/presence")
107 * 2.) confBasePath ("/usr/share/phosphor-fan-presence/presence")
108 * 3.) From first dbus object found hosting
109 * interface = Interface set in confDbusIntf
110 * property = Property set in confDbusProp
111 */
112 const fs::path getConfFile();
113
114 /**
Matthew Barthf3e70472019-12-03 13:33:20 -0600115 * @brief Load the json config file
116 */
117 void load();
118
119 /**
Matthew Barth4a94dec2019-11-15 10:40:47 -0600120 * @brief Process the json config to extract the defined fan presence
121 * policies.
122 *
123 * @param[in] jsonConf - parsed json configuration data
124 */
125 void process(const json& jsonConf);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600126
127 /**
Matthew Barthf3e70472019-12-03 13:33:20 -0600128 * @brief Get the redundancy policy of presence detection for a fan
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600129 *
Matthew Barthf3e70472019-12-03 13:33:20 -0600130 * @param[in] rpolicy - policy type to construct
131 * @param[in] fpolicy - fan policy object
132 *
133 * @return - The constructed redundancy policy type for the fan
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600134 */
Matthew Barthf3e70472019-12-03 13:33:20 -0600135 std::unique_ptr<RedundancyPolicy> getPolicy(const json& rpolicy,
136 const fanPolicy& fpolicy);
Matthew Barthfd05d642019-11-14 15:01:57 -0600137};
138
Matthew Barthe7566632019-11-18 16:13:04 -0600139/**
140 * Methods of fan presence detection function declarations
141 */
142namespace method
143{
144 /**
145 * @brief Fan presence detection method by tach feedback
146 *
147 * @param[in] fanIndex - fan object index to add tach method
148 * @param[in] method - json properties for a tach method
149 *
150 * @return - A presence sensor to detect fan presence by tach feedback
151 */
152 std::unique_ptr<PresenceSensor> getTach(size_t fanIndex,
153 const json& method);
154
155 /**
156 * @brief Fan presence detection method by gpio
157 *
158 * @param[in] fanIndex - fan object index to add gpio method
159 * @param[in] method - json properties for a gpio method
160 *
161 * @return - A presence sensor to detect fan presence by gpio
162 */
163 std::unique_ptr<PresenceSensor> getGpio(size_t fanIndex,
164 const json& method);
165
166} // namespace method
167
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600168/**
169 * Redundancy policies for fan presence detection function declarations
170 */
171namespace rpolicy
172{
173 /**
174 * @brief Create an `Anyof` redundancy policy on the created presence
175 * sensors for a fan
176 *
177 * @param[in] fan - fan policy object with the presence sensors for the fan
178 *
179 * @return - An `Anyof` redundancy policy
180 */
181 std::unique_ptr<RedundancyPolicy> getAnyof(const fanPolicy& fan);
182
183 /**
184 * @brief Create a `Fallback` redundancy policy on the created presence
185 * sensors for a fan
186 *
187 * @param[in] fan - fan policy object with the presence sensors for the fan
188 *
189 * @return - A `Fallback` redundancy policy
190 */
191 std::unique_ptr<RedundancyPolicy> getFallback(const fanPolicy& fan);
192
193} // namespace policy
194
Matthew Barthfd05d642019-11-14 15:01:57 -0600195} // namespace presence
196} // namespace fan
197} // namespace phosphor