blob: 38c76b92ed4a0de2a7b4190128dcf7208170a478 [file] [log] [blame]
Matthew Barthfd05d642019-11-14 15:01:57 -06001#pragma once
2
Matt Spinlere8122392020-09-24 13:22:18 -05003#include "error_reporter.hpp"
Matthew Barth4a94dec2019-11-15 10:40:47 -06004#include "fan.hpp"
Matthew Barthe7566632019-11-18 16:13:04 -06005#include "psensor.hpp"
Matthew Barth2d2caa32020-05-26 11:07:24 -05006#include "rpolicy.hpp"
7
8#include <nlohmann/json.hpp>
9#include <sdbusplus/bus.hpp>
10#include <sdeventplus/source/signal.hpp>
11
12#include <filesystem>
13#include <memory>
14#include <string>
15#include <vector>
Matthew Barthfd05d642019-11-14 15:01:57 -060016
17namespace phosphor
18{
19namespace fan
20{
21namespace presence
22{
23
Matthew Barthf3e70472019-12-03 13:33:20 -060024namespace fs = std::filesystem;
Matthew Barth4a94dec2019-11-15 10:40:47 -060025using json = nlohmann::json;
Matthew Barthf6d7f612019-12-10 15:22:54 -060026
Matthew Barth5060b102019-12-16 10:46:35 -060027constexpr auto confFileName = "config.json";
Jolie Ku1a568652020-08-24 16:32:15 +080028constexpr auto confAppName = "presence";
Matthew Barthf6d7f612019-12-10 15:22:54 -060029
Matthew Barthfd05d642019-11-14 15:01:57 -060030using policies = std::vector<std::unique_ptr<RedundancyPolicy>>;
Matthew Barthaa8d81d2019-11-21 14:07:31 -060031
32constexpr auto fanPolicyFanPos = 0;
33constexpr auto fanPolicySensorListPos = 1;
34using fanPolicy = std::tuple<Fan, std::vector<std::unique_ptr<PresenceSensor>>>;
35
Matthew Barthe7566632019-11-18 16:13:04 -060036// Presence method handler function
Matthew Barth2d2caa32020-05-26 11:07:24 -050037using methodHandler =
38 std::function<std::unique_ptr<PresenceSensor>(size_t, const json&)>;
Matthew Barthaa8d81d2019-11-21 14:07:31 -060039// Presence redundancy policy handler function
Matthew Barth2d2caa32020-05-26 11:07:24 -050040using rpolicyHandler =
41 std::function<std::unique_ptr<RedundancyPolicy>(const fanPolicy&)>;
Matthew Barthfd05d642019-11-14 15:01:57 -060042
43class JsonConfig
44{
Matthew Barth2d2caa32020-05-26 11:07:24 -050045 public:
46 JsonConfig() = delete;
47 JsonConfig(const JsonConfig&) = delete;
48 JsonConfig(JsonConfig&&) = delete;
49 JsonConfig& operator=(const JsonConfig&) = delete;
50 JsonConfig& operator=(JsonConfig&&) = delete;
51 ~JsonConfig() = default;
Matthew Barthfd05d642019-11-14 15:01:57 -060052
Matthew Barth2d2caa32020-05-26 11:07:24 -050053 /**
54 * Constructor
55 * Parses and populates the fan presence policies from a json file
56 *
57 * @param[in] bus - sdbusplus bus object
58 */
59 explicit JsonConfig(sdbusplus::bus::bus& bus);
Matthew Barthfd05d642019-11-14 15:01:57 -060060
Matthew Barth2d2caa32020-05-26 11:07:24 -050061 /**
62 * @brief Get the json config based fan presence policies
63 *
64 * @return - The fan presence policies
65 */
66 static const policies& get();
Matthew Barthfd05d642019-11-14 15:01:57 -060067
Matthew Barth2d2caa32020-05-26 11:07:24 -050068 /**
69 * @brief Callback function to handle receiving a HUP signal to
70 * reload the json configuration.
71 *
72 * @param[in] sigSrc - sd_event_source signal wrapper
73 * @param[in] sigInfo - signal info on signal fd
74 */
75 void sighupHandler(sdeventplus::source::Signal& sigSrc,
76 const struct signalfd_siginfo* sigInfo);
Matthew Barthfd05d642019-11-14 15:01:57 -060077
Matthew Barth2d2caa32020-05-26 11:07:24 -050078 private:
79 /* Fan presence policies */
80 static policies _policies;
Matthew Barthf3e70472019-12-03 13:33:20 -060081
Matthew Barth2d2caa32020-05-26 11:07:24 -050082 /* The sdbusplus bus object */
83 sdbusplus::bus::bus& _bus;
Matthew Barthfd05d642019-11-14 15:01:57 -060084
Matthew Barth2d2caa32020-05-26 11:07:24 -050085 /* List of Fan objects to have presence policies */
86 std::vector<fanPolicy> _fans;
Matthew Barth5060b102019-12-16 10:46:35 -060087
Matthew Barth2d2caa32020-05-26 11:07:24 -050088 /* Presence methods mapping to their associated handler function */
89 static const std::map<std::string, methodHandler> _methods;
Matthew Barthf3e70472019-12-03 13:33:20 -060090
Matthew Barth2d2caa32020-05-26 11:07:24 -050091 /**
92 * Presence redundancy policy mapping to their associated handler
93 * function
94 */
95 static const std::map<std::string, rpolicyHandler> _rpolicies;
Matthew Barth4a94dec2019-11-15 10:40:47 -060096
Matthew Barth2d2caa32020-05-26 11:07:24 -050097 /**
Matt Spinlere8122392020-09-24 13:22:18 -050098 * Class that handles reporting errors for missing fans.
99 */
100 std::unique_ptr<ErrorReporter> _reporter;
101
102 /**
Matthew Barth2d2caa32020-05-26 11:07:24 -0500103 * @brief Process the json config to extract the defined fan presence
104 * policies.
105 *
106 * @param[in] jsonConf - parsed json configuration data
107 */
108 void process(const json& jsonConf);
Matthew Barth5060b102019-12-16 10:46:35 -0600109
Matthew Barth2d2caa32020-05-26 11:07:24 -0500110 /**
111 * @brief Get the redundancy policy of presence detection for a fan
112 *
113 * @param[in] rpolicy - policy type to construct
114 * @param[in] fpolicy - fan policy object
115 *
116 * @return - The constructed redundancy policy type for the fan
117 */
118 std::unique_ptr<RedundancyPolicy> getPolicy(const json& rpolicy,
119 const fanPolicy& fpolicy);
Matthew Barthfd05d642019-11-14 15:01:57 -0600120};
121
Matthew Barthe7566632019-11-18 16:13:04 -0600122/**
123 * Methods of fan presence detection function declarations
124 */
125namespace method
126{
Matthew Barth2d2caa32020-05-26 11:07:24 -0500127/**
128 * @brief Fan presence detection method by tach feedback
129 *
130 * @param[in] fanIndex - fan object index to add tach method
131 * @param[in] method - json properties for a tach method
132 *
133 * @return - A presence sensor to detect fan presence by tach feedback
134 */
135std::unique_ptr<PresenceSensor> getTach(size_t fanIndex, const json& method);
Matthew Barthe7566632019-11-18 16:13:04 -0600136
Matthew Barth2d2caa32020-05-26 11:07:24 -0500137/**
138 * @brief Fan presence detection method by gpio
139 *
140 * @param[in] fanIndex - fan object index to add gpio method
141 * @param[in] method - json properties for a gpio method
142 *
143 * @return - A presence sensor to detect fan presence by gpio
144 */
145std::unique_ptr<PresenceSensor> getGpio(size_t fanIndex, const json& method);
Matthew Barthe7566632019-11-18 16:13:04 -0600146
147} // namespace method
148
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600149/**
150 * Redundancy policies for fan presence detection function declarations
151 */
152namespace rpolicy
153{
Matthew Barth2d2caa32020-05-26 11:07:24 -0500154/**
155 * @brief Create an `Anyof` redundancy policy on the created presence
156 * sensors for a fan
157 *
158 * @param[in] fan - fan policy object with the presence sensors for the fan
159 *
160 * @return - An `Anyof` redundancy policy
161 */
162std::unique_ptr<RedundancyPolicy> getAnyof(const fanPolicy& fan);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600163
Matthew Barth2d2caa32020-05-26 11:07:24 -0500164/**
165 * @brief Create a `Fallback` redundancy policy on the created presence
166 * sensors for a fan
167 *
168 * @param[in] fan - fan policy object with the presence sensors for the fan
169 *
170 * @return - A `Fallback` redundancy policy
171 */
172std::unique_ptr<RedundancyPolicy> getFallback(const fanPolicy& fan);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600173
Matthew Barth2d2caa32020-05-26 11:07:24 -0500174} // namespace rpolicy
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600175
Matthew Barthfd05d642019-11-14 15:01:57 -0600176} // namespace presence
177} // namespace fan
178} // namespace phosphor