blob: c41b8d5693b44343401c8cdf27e6dd5343e82850 [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 Barthfd05d642019-11-14 15:01:57 -06007
8#include "config.h"
9#include "rpolicy.hpp"
Matthew Barth4a94dec2019-11-15 10:40:47 -060010#include "fan.hpp"
Matthew Barthe7566632019-11-18 16:13:04 -060011#include "psensor.hpp"
Matthew Barthfd05d642019-11-14 15:01:57 -060012
13namespace phosphor
14{
15namespace fan
16{
17namespace presence
18{
19
Matthew Barth4a94dec2019-11-15 10:40:47 -060020using json = nlohmann::json;
Matthew Barthfd05d642019-11-14 15:01:57 -060021using policies = std::vector<std::unique_ptr<RedundancyPolicy>>;
Matthew Barthe7566632019-11-18 16:13:04 -060022// Presence method handler function
23using methodHandler = std::function<
24 std::unique_ptr<PresenceSensor>(size_t, const json&)>;
Matthew Barthfd05d642019-11-14 15:01:57 -060025
26class JsonConfig
27{
28 public:
29
30 JsonConfig() = delete;
31 JsonConfig(const JsonConfig&) = delete;
32 JsonConfig(JsonConfig&&) = delete;
33 JsonConfig& operator=(const JsonConfig&) = delete;
34 JsonConfig& operator=(JsonConfig&&) = delete;
35 ~JsonConfig() = default;
36
37 /**
38 * Constructor
39 * Parses and populates the fan presence policies from a json file
40 *
41 * @param[in] jsonFile - json configuration file
42 */
43 explicit JsonConfig(const std::string& jsonFile);
44
45 /**
46 * @brief Get the json config based fan presence policies
47 *
48 * @return - The fan presence policies
49 */
50 static const policies& get();
51
52 private:
53
54 /* Fan presence policies */
55 static policies _policies;
Matthew Barth4a94dec2019-11-15 10:40:47 -060056
57 /* List of Fan objects to have presence policies */
58 std::vector<Fan> _fans;
59
Matthew Barthe7566632019-11-18 16:13:04 -060060 /* Presence methods mapping to their associated handler function */
61 static const std::map<std::string, methodHandler> _methods;
62
63 /* List of fan presence sensors */
64 std::vector<std::unique_ptr<PresenceSensor>> _sensors;
65
Matthew Barth4a94dec2019-11-15 10:40:47 -060066 /**
67 * @brief Process the json config to extract the defined fan presence
68 * policies.
69 *
70 * @param[in] jsonConf - parsed json configuration data
71 */
72 void process(const json& jsonConf);
Matthew Barthfd05d642019-11-14 15:01:57 -060073};
74
Matthew Barthe7566632019-11-18 16:13:04 -060075/**
76 * Methods of fan presence detection function declarations
77 */
78namespace method
79{
80 /**
81 * @brief Fan presence detection method by tach feedback
82 *
83 * @param[in] fanIndex - fan object index to add tach method
84 * @param[in] method - json properties for a tach method
85 *
86 * @return - A presence sensor to detect fan presence by tach feedback
87 */
88 std::unique_ptr<PresenceSensor> getTach(size_t fanIndex,
89 const json& method);
90
91 /**
92 * @brief Fan presence detection method by gpio
93 *
94 * @param[in] fanIndex - fan object index to add gpio method
95 * @param[in] method - json properties for a gpio method
96 *
97 * @return - A presence sensor to detect fan presence by gpio
98 */
99 std::unique_ptr<PresenceSensor> getGpio(size_t fanIndex,
100 const json& method);
101
102} // namespace method
103
Matthew Barthfd05d642019-11-14 15:01:57 -0600104} // namespace presence
105} // namespace fan
106} // namespace phosphor