blob: 1e23380493d2e54cb5a816d7040e225dc4cb1cc5 [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
Matt Spinlerbc4179e2022-10-04 15:15:06 -050040using rpolicyHandler = std::function<std::unique_ptr<RedundancyPolicy>(
41 const fanPolicy&, std::unique_ptr<EEPROMDevice>)>;
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
Matthew Barth2d2caa32020-05-26 11:07:24 -050055 *
56 * @param[in] bus - sdbusplus bus object
57 */
Patrick Williamscb356d42022-07-22 19:26:53 -050058 explicit JsonConfig(sdbusplus::bus_t& 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 */
Mike Capps808d7fe2022-06-13 10:12:16 -040074 void sighupHandler(sdeventplus::source::Signal& /*sigSrc*/,
75 const struct signalfd_siginfo* /*sigInfo*/);
Matthew Barthfd05d642019-11-14 15:01:57 -060076
Matt Spinler0daedd12021-01-25 14:46:03 -060077 /**
78 * @brief Parses and populates the fan presence policies from
79 * the json file and then starts the actual presence
80 * detecting.
Matt Spinler0daedd12021-01-25 14:46:03 -060081 */
Matthew Barth5b839912021-06-21 14:46:34 -050082 void start();
Matt Spinler0daedd12021-01-25 14:46:03 -060083
Matthew Barth2d2caa32020-05-26 11:07:24 -050084 private:
85 /* Fan presence policies */
86 static policies _policies;
Matthew Barthf3e70472019-12-03 13:33:20 -060087
Matthew Barth2d2caa32020-05-26 11:07:24 -050088 /* The sdbusplus bus object */
Patrick Williamscb356d42022-07-22 19:26:53 -050089 sdbusplus::bus_t& _bus;
Matthew Barthfd05d642019-11-14 15:01:57 -060090
Matthew Barth2d2caa32020-05-26 11:07:24 -050091 /* List of Fan objects to have presence policies */
92 std::vector<fanPolicy> _fans;
Matthew Barth5060b102019-12-16 10:46:35 -060093
Matthew Barth2d2caa32020-05-26 11:07:24 -050094 /* Presence methods mapping to their associated handler function */
95 static const std::map<std::string, methodHandler> _methods;
Matthew Barthf3e70472019-12-03 13:33:20 -060096
Matthew Barth2d2caa32020-05-26 11:07:24 -050097 /**
98 * Presence redundancy policy mapping to their associated handler
99 * function
100 */
101 static const std::map<std::string, rpolicyHandler> _rpolicies;
Matthew Barth4a94dec2019-11-15 10:40:47 -0600102
Matthew Barth2d2caa32020-05-26 11:07:24 -0500103 /**
Matt Spinlere8122392020-09-24 13:22:18 -0500104 * Class that handles reporting errors for missing fans.
105 */
106 std::unique_ptr<ErrorReporter> _reporter;
107
108 /**
Matt Spinlerdfc8c4d2022-06-22 16:52:27 -0500109 * Tracks if the config has already been loaded.
110 */
111 bool _loaded = false;
112
113 /**
Matthew Barth2d2caa32020-05-26 11:07:24 -0500114 * @brief Process the json config to extract the defined fan presence
115 * policies.
116 *
117 * @param[in] jsonConf - parsed json configuration data
118 */
119 void process(const json& jsonConf);
Matthew Barth5060b102019-12-16 10:46:35 -0600120
Matthew Barth2d2caa32020-05-26 11:07:24 -0500121 /**
122 * @brief Get the redundancy policy of presence detection for a fan
123 *
124 * @param[in] rpolicy - policy type to construct
125 * @param[in] fpolicy - fan policy object
Matt Spinlerbc4179e2022-10-04 15:15:06 -0500126 * @param[in] eepromDevice - EEPROM device object
Matthew Barth2d2caa32020-05-26 11:07:24 -0500127 *
128 * @return - The constructed redundancy policy type for the fan
129 */
Matt Spinlerbc4179e2022-10-04 15:15:06 -0500130 std::unique_ptr<RedundancyPolicy>
131 getPolicy(const json& rpolicy, const fanPolicy& fpolicy,
132 std::unique_ptr<EEPROMDevice> eepromDevice);
Matthew Barthfd05d642019-11-14 15:01:57 -0600133};
134
Matthew Barthe7566632019-11-18 16:13:04 -0600135/**
136 * Methods of fan presence detection function declarations
137 */
138namespace method
139{
Matthew Barth2d2caa32020-05-26 11:07:24 -0500140/**
141 * @brief Fan presence detection method by tach feedback
142 *
143 * @param[in] fanIndex - fan object index to add tach method
144 * @param[in] method - json properties for a tach method
145 *
146 * @return - A presence sensor to detect fan presence by tach feedback
147 */
148std::unique_ptr<PresenceSensor> getTach(size_t fanIndex, const json& method);
Matthew Barthe7566632019-11-18 16:13:04 -0600149
Matthew Barth2d2caa32020-05-26 11:07:24 -0500150/**
151 * @brief Fan presence detection method by gpio
152 *
153 * @param[in] fanIndex - fan object index to add gpio method
154 * @param[in] method - json properties for a gpio method
155 *
156 * @return - A presence sensor to detect fan presence by gpio
157 */
158std::unique_ptr<PresenceSensor> getGpio(size_t fanIndex, const json& method);
Matthew Barthe7566632019-11-18 16:13:04 -0600159
160} // namespace method
161
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600162/**
163 * Redundancy policies for fan presence detection function declarations
164 */
165namespace rpolicy
166{
Matthew Barth2d2caa32020-05-26 11:07:24 -0500167/**
168 * @brief Create an `Anyof` redundancy policy on the created presence
169 * sensors for a fan
170 *
171 * @param[in] fan - fan policy object with the presence sensors for the fan
Matt Spinlerbc4179e2022-10-04 15:15:06 -0500172 * @param[in] eepromDevice - EEPROM device object
Matthew Barth2d2caa32020-05-26 11:07:24 -0500173 *
174 * @return - An `Anyof` redundancy policy
175 */
Matt Spinlerbc4179e2022-10-04 15:15:06 -0500176std::unique_ptr<RedundancyPolicy>
177 getAnyof(const fanPolicy& fan, std::unique_ptr<EEPROMDevice> eepromDevice);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600178
Matthew Barth2d2caa32020-05-26 11:07:24 -0500179/**
180 * @brief Create a `Fallback` redundancy policy on the created presence
181 * sensors for a fan
182 *
183 * @param[in] fan - fan policy object with the presence sensors for the fan
Matt Spinlerbc4179e2022-10-04 15:15:06 -0500184 * @param[in] eepromDevice - EEPROM device object
Matthew Barth2d2caa32020-05-26 11:07:24 -0500185 *
186 * @return - A `Fallback` redundancy policy
187 */
Patrick Williamsdfddd642024-08-16 15:21:51 -0400188std::unique_ptr<RedundancyPolicy> getFallback(
189 const fanPolicy& fan, std::unique_ptr<EEPROMDevice> eepromDevice);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600190
Matthew Barth2d2caa32020-05-26 11:07:24 -0500191} // namespace rpolicy
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600192
Matthew Barthfd05d642019-11-14 15:01:57 -0600193} // namespace presence
194} // namespace fan
195} // namespace phosphor