blob: 206b29087d1f335fd7aced56bd87cd69941c48e2 [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 Barthfd05d642019-11-14 15:01:57 -06009
10#include "config.h"
11#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
25constexpr auto jsonFileName = "config.json";
26constexpr auto jsonOverridePath = "/etc/phosphor-fan-presence/presence";
27
Matthew Barthfd05d642019-11-14 15:01:57 -060028using policies = std::vector<std::unique_ptr<RedundancyPolicy>>;
Matthew Barthaa8d81d2019-11-21 14:07:31 -060029
30constexpr auto fanPolicyFanPos = 0;
31constexpr auto fanPolicySensorListPos = 1;
32using fanPolicy = std::tuple<Fan, std::vector<std::unique_ptr<PresenceSensor>>>;
33
Matthew Barthe7566632019-11-18 16:13:04 -060034// Presence method handler function
35using methodHandler = std::function<
36 std::unique_ptr<PresenceSensor>(size_t, const json&)>;
Matthew Barthaa8d81d2019-11-21 14:07:31 -060037// Presence redundancy policy handler function
38using rpolicyHandler = std::function<
39 std::unique_ptr<RedundancyPolicy>(const fanPolicy&)>;
Matthew Barthfd05d642019-11-14 15:01:57 -060040
41class JsonConfig
42{
43 public:
44
45 JsonConfig() = delete;
46 JsonConfig(const JsonConfig&) = delete;
47 JsonConfig(JsonConfig&&) = delete;
48 JsonConfig& operator=(const JsonConfig&) = delete;
49 JsonConfig& operator=(JsonConfig&&) = delete;
50 ~JsonConfig() = default;
51
52 /**
53 * Constructor
54 * Parses and populates the fan presence policies from a json file
55 *
Matthew Barthf6d7f612019-12-10 15:22:54 -060056 * @param[in] jsonConfigPath - json configuration path
Matthew Barthfd05d642019-11-14 15:01:57 -060057 */
Matthew Barthf6d7f612019-12-10 15:22:54 -060058 explicit JsonConfig(const std::string& jsonConfigPath);
Matthew Barthfd05d642019-11-14 15:01:57 -060059
60 /**
61 * @brief Get the json config based fan presence policies
62 *
63 * @return - The fan presence policies
64 */
65 static const policies& get();
66
Matthew Barthf3e70472019-12-03 13:33:20 -060067 /**
68 * @brief Callback function to handle receiving a HUP signal to
69 * reload the json configuration.
70 *
71 * @param[in] sigSrc - sd_event_source signal wrapper
72 * @param[in] sigInfo - signal info on signal fd
73 */
74 void sighupHandler(sdeventplus::source::Signal& sigSrc,
75 const struct signalfd_siginfo* sigInfo);
76
Matthew Barthfd05d642019-11-14 15:01:57 -060077 private:
78
79 /* Fan presence policies */
80 static policies _policies;
Matthew Barth4a94dec2019-11-15 10:40:47 -060081
Matthew Barthf3e70472019-12-03 13:33:20 -060082 /* Default json configuration file */
83 const fs::path _defaultFile;
84
Matthew Barth4a94dec2019-11-15 10:40:47 -060085 /* List of Fan objects to have presence policies */
Matthew Barthaa8d81d2019-11-21 14:07:31 -060086 std::vector<fanPolicy> _fans;
Matthew Barth4a94dec2019-11-15 10:40:47 -060087
Matthew Barthe7566632019-11-18 16:13:04 -060088 /* Presence methods mapping to their associated handler function */
89 static const std::map<std::string, methodHandler> _methods;
90
Matthew Barthaa8d81d2019-11-21 14:07:31 -060091 /**
92 * Presence redundancy policy mapping to their associated handler
93 * function
94 */
95 static const std::map<std::string, rpolicyHandler> _rpolicies;
Matthew Barthe7566632019-11-18 16:13:04 -060096
Matthew Barth4a94dec2019-11-15 10:40:47 -060097 /**
Matthew Barthf3e70472019-12-03 13:33:20 -060098 * @brief Load the json config file
99 */
100 void load();
101
102 /**
Matthew Barth4a94dec2019-11-15 10:40:47 -0600103 * @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 Barthaa8d81d2019-11-21 14:07:31 -0600109
110 /**
Matthew Barthf3e70472019-12-03 13:33:20 -0600111 * @brief Get the redundancy policy of presence detection for a fan
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600112 *
Matthew Barthf3e70472019-12-03 13:33:20 -0600113 * @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
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600117 */
Matthew Barthf3e70472019-12-03 13:33:20 -0600118 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{
127 /**
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 */
135 std::unique_ptr<PresenceSensor> getTach(size_t fanIndex,
136 const json& method);
137
138 /**
139 * @brief Fan presence detection method by gpio
140 *
141 * @param[in] fanIndex - fan object index to add gpio method
142 * @param[in] method - json properties for a gpio method
143 *
144 * @return - A presence sensor to detect fan presence by gpio
145 */
146 std::unique_ptr<PresenceSensor> getGpio(size_t fanIndex,
147 const json& method);
148
149} // namespace method
150
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600151/**
152 * Redundancy policies for fan presence detection function declarations
153 */
154namespace rpolicy
155{
156 /**
157 * @brief Create an `Anyof` redundancy policy on the created presence
158 * sensors for a fan
159 *
160 * @param[in] fan - fan policy object with the presence sensors for the fan
161 *
162 * @return - An `Anyof` redundancy policy
163 */
164 std::unique_ptr<RedundancyPolicy> getAnyof(const fanPolicy& fan);
165
166 /**
167 * @brief Create a `Fallback` redundancy policy on the created presence
168 * sensors for a fan
169 *
170 * @param[in] fan - fan policy object with the presence sensors for the fan
171 *
172 * @return - A `Fallback` redundancy policy
173 */
174 std::unique_ptr<RedundancyPolicy> getFallback(const fanPolicy& fan);
175
176} // namespace policy
177
Matthew Barthfd05d642019-11-14 15:01:57 -0600178} // namespace presence
179} // namespace fan
180} // namespace phosphor