blob: 9d99a4098d4d833d81490c54441bfdeb4b96cdb3 [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";
Jolie Ku1a568652020-08-24 16:32:15 +080027constexpr auto confAppName = "presence";
Matthew Barthf6d7f612019-12-10 15:22:54 -060028
Matthew Barthfd05d642019-11-14 15:01:57 -060029using policies = std::vector<std::unique_ptr<RedundancyPolicy>>;
Matthew Barthaa8d81d2019-11-21 14:07:31 -060030
31constexpr auto fanPolicyFanPos = 0;
32constexpr auto fanPolicySensorListPos = 1;
33using fanPolicy = std::tuple<Fan, std::vector<std::unique_ptr<PresenceSensor>>>;
34
Matthew Barthe7566632019-11-18 16:13:04 -060035// Presence method handler function
Matthew Barth2d2caa32020-05-26 11:07:24 -050036using methodHandler =
37 std::function<std::unique_ptr<PresenceSensor>(size_t, const json&)>;
Matthew Barthaa8d81d2019-11-21 14:07:31 -060038// Presence redundancy policy handler function
Matthew Barth2d2caa32020-05-26 11:07:24 -050039using rpolicyHandler =
40 std::function<std::unique_ptr<RedundancyPolicy>(const fanPolicy&)>;
Matthew Barthfd05d642019-11-14 15:01:57 -060041
42class JsonConfig
43{
Matthew Barth2d2caa32020-05-26 11:07:24 -050044 public:
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;
Matthew Barthfd05d642019-11-14 15:01:57 -060051
Matthew Barth2d2caa32020-05-26 11:07:24 -050052 /**
53 * Constructor
54 * Parses and populates the fan presence policies from a json file
55 *
56 * @param[in] bus - sdbusplus bus object
57 */
58 explicit JsonConfig(sdbusplus::bus::bus& bus);
Matthew Barthfd05d642019-11-14 15:01:57 -060059
Matthew Barth2d2caa32020-05-26 11:07:24 -050060 /**
61 * @brief Get the json config based fan presence policies
62 *
63 * @return - The fan presence policies
64 */
65 static const policies& get();
Matthew Barthfd05d642019-11-14 15:01:57 -060066
Matthew Barth2d2caa32020-05-26 11:07:24 -050067 /**
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);
Matthew Barthfd05d642019-11-14 15:01:57 -060076
Matthew Barth2d2caa32020-05-26 11:07:24 -050077 private:
78 /* Fan presence policies */
79 static policies _policies;
Matthew Barthf3e70472019-12-03 13:33:20 -060080
Matthew Barth2d2caa32020-05-26 11:07:24 -050081 /* The sdbusplus bus object */
82 sdbusplus::bus::bus& _bus;
Matthew Barthfd05d642019-11-14 15:01:57 -060083
Matthew Barth2d2caa32020-05-26 11:07:24 -050084 /* List of Fan objects to have presence policies */
85 std::vector<fanPolicy> _fans;
Matthew Barth5060b102019-12-16 10:46:35 -060086
Matthew Barth2d2caa32020-05-26 11:07:24 -050087 /* Presence methods mapping to their associated handler function */
88 static const std::map<std::string, methodHandler> _methods;
Matthew Barthf3e70472019-12-03 13:33:20 -060089
Matthew Barth2d2caa32020-05-26 11:07:24 -050090 /**
91 * Presence redundancy policy mapping to their associated handler
92 * function
93 */
94 static const std::map<std::string, rpolicyHandler> _rpolicies;
Matthew Barth4a94dec2019-11-15 10:40:47 -060095
Matthew Barth2d2caa32020-05-26 11:07:24 -050096 /**
Matthew Barth2d2caa32020-05-26 11:07:24 -050097 * @brief Process the json config to extract the defined fan presence
98 * policies.
99 *
100 * @param[in] jsonConf - parsed json configuration data
101 */
102 void process(const json& jsonConf);
Matthew Barth5060b102019-12-16 10:46:35 -0600103
Matthew Barth2d2caa32020-05-26 11:07:24 -0500104 /**
105 * @brief Get the redundancy policy of presence detection for a fan
106 *
107 * @param[in] rpolicy - policy type to construct
108 * @param[in] fpolicy - fan policy object
109 *
110 * @return - The constructed redundancy policy type for the fan
111 */
112 std::unique_ptr<RedundancyPolicy> getPolicy(const json& rpolicy,
113 const fanPolicy& fpolicy);
Matthew Barthfd05d642019-11-14 15:01:57 -0600114};
115
Matthew Barthe7566632019-11-18 16:13:04 -0600116/**
117 * Methods of fan presence detection function declarations
118 */
119namespace method
120{
Matthew Barth2d2caa32020-05-26 11:07:24 -0500121/**
122 * @brief Fan presence detection method by tach feedback
123 *
124 * @param[in] fanIndex - fan object index to add tach method
125 * @param[in] method - json properties for a tach method
126 *
127 * @return - A presence sensor to detect fan presence by tach feedback
128 */
129std::unique_ptr<PresenceSensor> getTach(size_t fanIndex, const json& method);
Matthew Barthe7566632019-11-18 16:13:04 -0600130
Matthew Barth2d2caa32020-05-26 11:07:24 -0500131/**
132 * @brief Fan presence detection method by gpio
133 *
134 * @param[in] fanIndex - fan object index to add gpio method
135 * @param[in] method - json properties for a gpio method
136 *
137 * @return - A presence sensor to detect fan presence by gpio
138 */
139std::unique_ptr<PresenceSensor> getGpio(size_t fanIndex, const json& method);
Matthew Barthe7566632019-11-18 16:13:04 -0600140
141} // namespace method
142
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600143/**
144 * Redundancy policies for fan presence detection function declarations
145 */
146namespace rpolicy
147{
Matthew Barth2d2caa32020-05-26 11:07:24 -0500148/**
149 * @brief Create an `Anyof` redundancy policy on the created presence
150 * sensors for a fan
151 *
152 * @param[in] fan - fan policy object with the presence sensors for the fan
153 *
154 * @return - An `Anyof` redundancy policy
155 */
156std::unique_ptr<RedundancyPolicy> getAnyof(const fanPolicy& fan);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600157
Matthew Barth2d2caa32020-05-26 11:07:24 -0500158/**
159 * @brief Create a `Fallback` redundancy policy on the created presence
160 * sensors for a fan
161 *
162 * @param[in] fan - fan policy object with the presence sensors for the fan
163 *
164 * @return - A `Fallback` redundancy policy
165 */
166std::unique_ptr<RedundancyPolicy> getFallback(const fanPolicy& fan);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600167
Matthew Barth2d2caa32020-05-26 11:07:24 -0500168} // namespace rpolicy
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600169
Matthew Barthfd05d642019-11-14 15:01:57 -0600170} // namespace presence
171} // namespace fan
172} // namespace phosphor