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 | f6d7f61 | 2019-12-10 15:22:54 -0600 | [diff] [blame] | 24 | |
| 25 | constexpr auto jsonFileName = "config.json"; |
| 26 | constexpr auto jsonOverridePath = "/etc/phosphor-fan-presence/presence"; |
| 27 | |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 28 | using policies = std::vector<std::unique_ptr<RedundancyPolicy>>; |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 29 | |
| 30 | constexpr auto fanPolicyFanPos = 0; |
| 31 | constexpr auto fanPolicySensorListPos = 1; |
| 32 | using fanPolicy = std::tuple<Fan, std::vector<std::unique_ptr<PresenceSensor>>>; |
| 33 | |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 34 | // Presence method handler function |
| 35 | using methodHandler = std::function< |
| 36 | std::unique_ptr<PresenceSensor>(size_t, const json&)>; |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 37 | // Presence redundancy policy handler function |
| 38 | using rpolicyHandler = std::function< |
| 39 | std::unique_ptr<RedundancyPolicy>(const fanPolicy&)>; |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 40 | |
| 41 | class JsonConfig |
| 42 | { |
| 43 | public: |
| 44 | |
| 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; |
| 51 | |
| 52 | /** |
| 53 | * Constructor |
| 54 | * Parses and populates the fan presence policies from a json file |
| 55 | * |
Matthew Barth | f6d7f61 | 2019-12-10 15:22:54 -0600 | [diff] [blame] | 56 | * @param[in] jsonConfigPath - json configuration path |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 57 | */ |
Matthew Barth | f6d7f61 | 2019-12-10 15:22:54 -0600 | [diff] [blame] | 58 | explicit JsonConfig(const std::string& jsonConfigPath); |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * @brief Get the json config based fan presence policies |
| 62 | * |
| 63 | * @return - The fan presence policies |
| 64 | */ |
| 65 | static const policies& get(); |
| 66 | |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 67 | /** |
| 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); |
| 76 | |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 77 | private: |
| 78 | |
| 79 | /* Fan presence policies */ |
| 80 | static policies _policies; |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 81 | |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 82 | /* Default json configuration file */ |
| 83 | const fs::path _defaultFile; |
| 84 | |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 85 | /* List of Fan objects to have presence policies */ |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 86 | std::vector<fanPolicy> _fans; |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 87 | |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 88 | /* Presence methods mapping to their associated handler function */ |
| 89 | static const std::map<std::string, methodHandler> _methods; |
| 90 | |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 91 | /** |
| 92 | * Presence redundancy policy mapping to their associated handler |
| 93 | * function |
| 94 | */ |
| 95 | static const std::map<std::string, rpolicyHandler> _rpolicies; |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 96 | |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 97 | /** |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 98 | * @brief Load the json config file |
| 99 | */ |
| 100 | void load(); |
| 101 | |
| 102 | /** |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 103 | * @brief Process the json config to extract the defined fan presence |
| 104 | * policies. |
| 105 | * |
| 106 | * @param[in] jsonConf - parsed json configuration data |
| 107 | */ |
| 108 | void process(const json& jsonConf); |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 109 | |
| 110 | /** |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 111 | * @brief Get the redundancy policy of presence detection for a fan |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 112 | * |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 113 | * @param[in] rpolicy - policy type to construct |
| 114 | * @param[in] fpolicy - fan policy object |
| 115 | * |
| 116 | * @return - The constructed redundancy policy type for the fan |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 117 | */ |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 118 | std::unique_ptr<RedundancyPolicy> getPolicy(const json& rpolicy, |
| 119 | const fanPolicy& fpolicy); |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 120 | }; |
| 121 | |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 122 | /** |
| 123 | * Methods of fan presence detection function declarations |
| 124 | */ |
| 125 | namespace method |
| 126 | { |
| 127 | /** |
| 128 | * @brief Fan presence detection method by tach feedback |
| 129 | * |
| 130 | * @param[in] fanIndex - fan object index to add tach method |
| 131 | * @param[in] method - json properties for a tach method |
| 132 | * |
| 133 | * @return - A presence sensor to detect fan presence by tach feedback |
| 134 | */ |
| 135 | std::unique_ptr<PresenceSensor> getTach(size_t fanIndex, |
| 136 | const json& method); |
| 137 | |
| 138 | /** |
| 139 | * @brief Fan presence detection method by gpio |
| 140 | * |
| 141 | * @param[in] fanIndex - fan object index to add gpio method |
| 142 | * @param[in] method - json properties for a gpio method |
| 143 | * |
| 144 | * @return - A presence sensor to detect fan presence by gpio |
| 145 | */ |
| 146 | std::unique_ptr<PresenceSensor> getGpio(size_t fanIndex, |
| 147 | const json& method); |
| 148 | |
| 149 | } // namespace method |
| 150 | |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 151 | /** |
| 152 | * Redundancy policies for fan presence detection function declarations |
| 153 | */ |
| 154 | namespace rpolicy |
| 155 | { |
| 156 | /** |
| 157 | * @brief Create an `Anyof` redundancy policy on the created presence |
| 158 | * sensors for a fan |
| 159 | * |
| 160 | * @param[in] fan - fan policy object with the presence sensors for the fan |
| 161 | * |
| 162 | * @return - An `Anyof` redundancy policy |
| 163 | */ |
| 164 | std::unique_ptr<RedundancyPolicy> getAnyof(const fanPolicy& fan); |
| 165 | |
| 166 | /** |
| 167 | * @brief Create a `Fallback` redundancy policy on the created presence |
| 168 | * sensors for a fan |
| 169 | * |
| 170 | * @param[in] fan - fan policy object with the presence sensors for the fan |
| 171 | * |
| 172 | * @return - A `Fallback` redundancy policy |
| 173 | */ |
| 174 | std::unique_ptr<RedundancyPolicy> getFallback(const fanPolicy& fan); |
| 175 | |
| 176 | } // namespace policy |
| 177 | |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 178 | } // namespace presence |
| 179 | } // namespace fan |
| 180 | } // namespace phosphor |