Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <string> |
| 4 | #include <vector> |
| 5 | #include <memory> |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 6 | #include <nlohmann/json.hpp> |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame^] | 7 | #include <filesystem> |
| 8 | #include <sdeventplus/source/signal.hpp> |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 9 | |
| 10 | #include "config.h" |
| 11 | #include "rpolicy.hpp" |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 12 | #include "fan.hpp" |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 13 | #include "psensor.hpp" |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 14 | |
| 15 | namespace phosphor |
| 16 | { |
| 17 | namespace fan |
| 18 | { |
| 19 | namespace presence |
| 20 | { |
| 21 | |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame^] | 22 | namespace fs = std::filesystem; |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 23 | using json = nlohmann::json; |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 24 | using policies = std::vector<std::unique_ptr<RedundancyPolicy>>; |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 25 | |
| 26 | constexpr auto fanPolicyFanPos = 0; |
| 27 | constexpr auto fanPolicySensorListPos = 1; |
| 28 | using fanPolicy = std::tuple<Fan, std::vector<std::unique_ptr<PresenceSensor>>>; |
| 29 | |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 30 | // Presence method handler function |
| 31 | using methodHandler = std::function< |
| 32 | std::unique_ptr<PresenceSensor>(size_t, const json&)>; |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 33 | // Presence redundancy policy handler function |
| 34 | using rpolicyHandler = std::function< |
| 35 | std::unique_ptr<RedundancyPolicy>(const fanPolicy&)>; |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 36 | |
| 37 | class 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 Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame^] | 63 | /** |
| 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 Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 73 | private: |
| 74 | |
| 75 | /* Fan presence policies */ |
| 76 | static policies _policies; |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 77 | |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame^] | 78 | /* Default json configuration file */ |
| 79 | const fs::path _defaultFile; |
| 80 | |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 81 | /* List of Fan objects to have presence policies */ |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 82 | std::vector<fanPolicy> _fans; |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 83 | |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 84 | /* Presence methods mapping to their associated handler function */ |
| 85 | static const std::map<std::string, methodHandler> _methods; |
| 86 | |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 87 | /** |
| 88 | * Presence redundancy policy mapping to their associated handler |
| 89 | * function |
| 90 | */ |
| 91 | static const std::map<std::string, rpolicyHandler> _rpolicies; |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 92 | |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 93 | /** |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame^] | 94 | * @brief Load the json config file |
| 95 | */ |
| 96 | void load(); |
| 97 | |
| 98 | /** |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 99 | * @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 Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 105 | |
| 106 | /** |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame^] | 107 | * @brief Get the redundancy policy of presence detection for a fan |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 108 | * |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame^] | 109 | * @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 Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 113 | */ |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame^] | 114 | std::unique_ptr<RedundancyPolicy> getPolicy(const json& rpolicy, |
| 115 | const fanPolicy& fpolicy); |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 116 | }; |
| 117 | |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 118 | /** |
| 119 | * Methods of fan presence detection function declarations |
| 120 | */ |
| 121 | namespace 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 Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 147 | /** |
| 148 | * Redundancy policies for fan presence detection function declarations |
| 149 | */ |
| 150 | namespace 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 Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 174 | } // namespace presence |
| 175 | } // namespace fan |
| 176 | } // namespace phosphor |