blob: a369bd5cbe371e0d0c15cce34bdd1c842b5d6e6b [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 Barthfd05d642019-11-14 15:01:57 -060024using policies = std::vector<std::unique_ptr<RedundancyPolicy>>;
Matthew Barthaa8d81d2019-11-21 14:07:31 -060025
26constexpr auto fanPolicyFanPos = 0;
27constexpr auto fanPolicySensorListPos = 1;
28using fanPolicy = std::tuple<Fan, std::vector<std::unique_ptr<PresenceSensor>>>;
29
Matthew Barthe7566632019-11-18 16:13:04 -060030// Presence method handler function
31using methodHandler = std::function<
32 std::unique_ptr<PresenceSensor>(size_t, const json&)>;
Matthew Barthaa8d81d2019-11-21 14:07:31 -060033// Presence redundancy policy handler function
34using rpolicyHandler = std::function<
35 std::unique_ptr<RedundancyPolicy>(const fanPolicy&)>;
Matthew Barthfd05d642019-11-14 15:01:57 -060036
37class JsonConfig
38{
39 public:
40
41 JsonConfig() = delete;
42 JsonConfig(const JsonConfig&) = delete;
43 JsonConfig(JsonConfig&&) = delete;
44 JsonConfig& operator=(const JsonConfig&) = delete;
45 JsonConfig& operator=(JsonConfig&&) = delete;
46 ~JsonConfig() = default;
47
48 /**
49 * Constructor
50 * Parses and populates the fan presence policies from a json file
51 *
52 * @param[in] jsonFile - json configuration file
53 */
54 explicit JsonConfig(const std::string& jsonFile);
55
56 /**
57 * @brief Get the json config based fan presence policies
58 *
59 * @return - The fan presence policies
60 */
61 static const policies& get();
62
Matthew Barthf3e70472019-12-03 13:33:20 -060063 /**
64 * @brief Callback function to handle receiving a HUP signal to
65 * reload the json configuration.
66 *
67 * @param[in] sigSrc - sd_event_source signal wrapper
68 * @param[in] sigInfo - signal info on signal fd
69 */
70 void sighupHandler(sdeventplus::source::Signal& sigSrc,
71 const struct signalfd_siginfo* sigInfo);
72
Matthew Barthfd05d642019-11-14 15:01:57 -060073 private:
74
75 /* Fan presence policies */
76 static policies _policies;
Matthew Barth4a94dec2019-11-15 10:40:47 -060077
Matthew Barthf3e70472019-12-03 13:33:20 -060078 /* Default json configuration file */
79 const fs::path _defaultFile;
80
Matthew Barth4a94dec2019-11-15 10:40:47 -060081 /* List of Fan objects to have presence policies */
Matthew Barthaa8d81d2019-11-21 14:07:31 -060082 std::vector<fanPolicy> _fans;
Matthew Barth4a94dec2019-11-15 10:40:47 -060083
Matthew Barthe7566632019-11-18 16:13:04 -060084 /* Presence methods mapping to their associated handler function */
85 static const std::map<std::string, methodHandler> _methods;
86
Matthew Barthaa8d81d2019-11-21 14:07:31 -060087 /**
88 * Presence redundancy policy mapping to their associated handler
89 * function
90 */
91 static const std::map<std::string, rpolicyHandler> _rpolicies;
Matthew Barthe7566632019-11-18 16:13:04 -060092
Matthew Barth4a94dec2019-11-15 10:40:47 -060093 /**
Matthew Barthf3e70472019-12-03 13:33:20 -060094 * @brief Load the json config file
95 */
96 void load();
97
98 /**
Matthew Barth4a94dec2019-11-15 10:40:47 -060099 * @brief Process the json config to extract the defined fan presence
100 * policies.
101 *
102 * @param[in] jsonConf - parsed json configuration data
103 */
104 void process(const json& jsonConf);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600105
106 /**
Matthew Barthf3e70472019-12-03 13:33:20 -0600107 * @brief Get the redundancy policy of presence detection for a fan
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600108 *
Matthew Barthf3e70472019-12-03 13:33:20 -0600109 * @param[in] rpolicy - policy type to construct
110 * @param[in] fpolicy - fan policy object
111 *
112 * @return - The constructed redundancy policy type for the fan
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600113 */
Matthew Barthf3e70472019-12-03 13:33:20 -0600114 std::unique_ptr<RedundancyPolicy> getPolicy(const json& rpolicy,
115 const fanPolicy& fpolicy);
Matthew Barthfd05d642019-11-14 15:01:57 -0600116};
117
Matthew Barthe7566632019-11-18 16:13:04 -0600118/**
119 * Methods of fan presence detection function declarations
120 */
121namespace method
122{
123 /**
124 * @brief Fan presence detection method by tach feedback
125 *
126 * @param[in] fanIndex - fan object index to add tach method
127 * @param[in] method - json properties for a tach method
128 *
129 * @return - A presence sensor to detect fan presence by tach feedback
130 */
131 std::unique_ptr<PresenceSensor> getTach(size_t fanIndex,
132 const json& method);
133
134 /**
135 * @brief Fan presence detection method by gpio
136 *
137 * @param[in] fanIndex - fan object index to add gpio method
138 * @param[in] method - json properties for a gpio method
139 *
140 * @return - A presence sensor to detect fan presence by gpio
141 */
142 std::unique_ptr<PresenceSensor> getGpio(size_t fanIndex,
143 const json& method);
144
145} // namespace method
146
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600147/**
148 * Redundancy policies for fan presence detection function declarations
149 */
150namespace rpolicy
151{
152 /**
153 * @brief Create an `Anyof` redundancy policy on the created presence
154 * sensors for a fan
155 *
156 * @param[in] fan - fan policy object with the presence sensors for the fan
157 *
158 * @return - An `Anyof` redundancy policy
159 */
160 std::unique_ptr<RedundancyPolicy> getAnyof(const fanPolicy& fan);
161
162 /**
163 * @brief Create a `Fallback` redundancy policy on the created presence
164 * sensors for a fan
165 *
166 * @param[in] fan - fan policy object with the presence sensors for the fan
167 *
168 * @return - A `Fallback` redundancy policy
169 */
170 std::unique_ptr<RedundancyPolicy> getFallback(const fanPolicy& fan);
171
172} // namespace policy
173
Matthew Barthfd05d642019-11-14 15:01:57 -0600174} // namespace presence
175} // namespace fan
176} // namespace phosphor