Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Matt Spinler | e812239 | 2020-09-24 13:22:18 -0500 | [diff] [blame] | 3 | #include "error_reporter.hpp" |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 4 | #include "fan.hpp" |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 5 | #include "psensor.hpp" |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 6 | #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 Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 16 | |
| 17 | namespace phosphor |
| 18 | { |
| 19 | namespace fan |
| 20 | { |
| 21 | namespace presence |
| 22 | { |
| 23 | |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 24 | namespace fs = std::filesystem; |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 25 | using json = nlohmann::json; |
Matthew Barth | f6d7f61 | 2019-12-10 15:22:54 -0600 | [diff] [blame] | 26 | |
Matthew Barth | 5060b10 | 2019-12-16 10:46:35 -0600 | [diff] [blame] | 27 | constexpr auto confFileName = "config.json"; |
Jolie Ku | 1a56865 | 2020-08-24 16:32:15 +0800 | [diff] [blame] | 28 | constexpr auto confAppName = "presence"; |
Matthew Barth | f6d7f61 | 2019-12-10 15:22:54 -0600 | [diff] [blame] | 29 | |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 30 | using policies = std::vector<std::unique_ptr<RedundancyPolicy>>; |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 31 | |
| 32 | constexpr auto fanPolicyFanPos = 0; |
| 33 | constexpr auto fanPolicySensorListPos = 1; |
| 34 | using fanPolicy = std::tuple<Fan, std::vector<std::unique_ptr<PresenceSensor>>>; |
| 35 | |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 36 | // Presence method handler function |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 37 | using methodHandler = |
| 38 | std::function<std::unique_ptr<PresenceSensor>(size_t, const json&)>; |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 39 | // Presence redundancy policy handler function |
Matt Spinler | bc4179e | 2022-10-04 15:15:06 -0500 | [diff] [blame] | 40 | using rpolicyHandler = std::function<std::unique_ptr<RedundancyPolicy>( |
| 41 | const fanPolicy&, std::unique_ptr<EEPROMDevice>)>; |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 42 | |
| 43 | class JsonConfig |
| 44 | { |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 45 | 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 Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 52 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 53 | /** |
| 54 | * Constructor |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 55 | * |
| 56 | * @param[in] bus - sdbusplus bus object |
| 57 | */ |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 58 | explicit JsonConfig(sdbusplus::bus_t& bus); |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 59 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 60 | /** |
| 61 | * @brief Get the json config based fan presence policies |
| 62 | * |
| 63 | * @return - The fan presence policies |
| 64 | */ |
| 65 | static const policies& get(); |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 66 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [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 | */ |
Mike Capps | 808d7fe | 2022-06-13 10:12:16 -0400 | [diff] [blame] | 74 | void sighupHandler(sdeventplus::source::Signal& /*sigSrc*/, |
| 75 | const struct signalfd_siginfo* /*sigInfo*/); |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 76 | |
Matt Spinler | 0daedd1 | 2021-01-25 14:46:03 -0600 | [diff] [blame] | 77 | /** |
| 78 | * @brief Parses and populates the fan presence policies from |
| 79 | * the json file and then starts the actual presence |
| 80 | * detecting. |
Matt Spinler | 0daedd1 | 2021-01-25 14:46:03 -0600 | [diff] [blame] | 81 | */ |
Matthew Barth | 5b83991 | 2021-06-21 14:46:34 -0500 | [diff] [blame] | 82 | void start(); |
Matt Spinler | 0daedd1 | 2021-01-25 14:46:03 -0600 | [diff] [blame] | 83 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 84 | private: |
| 85 | /* Fan presence policies */ |
| 86 | static policies _policies; |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 87 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 88 | /* The sdbusplus bus object */ |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 89 | sdbusplus::bus_t& _bus; |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 90 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 91 | /* List of Fan objects to have presence policies */ |
| 92 | std::vector<fanPolicy> _fans; |
Matthew Barth | 5060b10 | 2019-12-16 10:46:35 -0600 | [diff] [blame] | 93 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 94 | /* Presence methods mapping to their associated handler function */ |
| 95 | static const std::map<std::string, methodHandler> _methods; |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 96 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 97 | /** |
| 98 | * Presence redundancy policy mapping to their associated handler |
| 99 | * function |
| 100 | */ |
| 101 | static const std::map<std::string, rpolicyHandler> _rpolicies; |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 102 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 103 | /** |
Matt Spinler | e812239 | 2020-09-24 13:22:18 -0500 | [diff] [blame] | 104 | * Class that handles reporting errors for missing fans. |
| 105 | */ |
| 106 | std::unique_ptr<ErrorReporter> _reporter; |
| 107 | |
| 108 | /** |
Matt Spinler | dfc8c4d | 2022-06-22 16:52:27 -0500 | [diff] [blame] | 109 | * Tracks if the config has already been loaded. |
| 110 | */ |
| 111 | bool _loaded = false; |
| 112 | |
| 113 | /** |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 114 | * @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 Barth | 5060b10 | 2019-12-16 10:46:35 -0600 | [diff] [blame] | 120 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 121 | /** |
| 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 Spinler | bc4179e | 2022-10-04 15:15:06 -0500 | [diff] [blame] | 126 | * @param[in] eepromDevice - EEPROM device object |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 127 | * |
| 128 | * @return - The constructed redundancy policy type for the fan |
| 129 | */ |
Matt Spinler | bc4179e | 2022-10-04 15:15:06 -0500 | [diff] [blame] | 130 | std::unique_ptr<RedundancyPolicy> |
| 131 | getPolicy(const json& rpolicy, const fanPolicy& fpolicy, |
| 132 | std::unique_ptr<EEPROMDevice> eepromDevice); |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 133 | }; |
| 134 | |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 135 | /** |
| 136 | * Methods of fan presence detection function declarations |
| 137 | */ |
| 138 | namespace method |
| 139 | { |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 140 | /** |
| 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 | */ |
| 148 | std::unique_ptr<PresenceSensor> getTach(size_t fanIndex, const json& method); |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 149 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 150 | /** |
| 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 | */ |
| 158 | std::unique_ptr<PresenceSensor> getGpio(size_t fanIndex, const json& method); |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 159 | |
| 160 | } // namespace method |
| 161 | |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 162 | /** |
| 163 | * Redundancy policies for fan presence detection function declarations |
| 164 | */ |
| 165 | namespace rpolicy |
| 166 | { |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 167 | /** |
| 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 Spinler | bc4179e | 2022-10-04 15:15:06 -0500 | [diff] [blame] | 172 | * @param[in] eepromDevice - EEPROM device object |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 173 | * |
| 174 | * @return - An `Anyof` redundancy policy |
| 175 | */ |
Matt Spinler | bc4179e | 2022-10-04 15:15:06 -0500 | [diff] [blame] | 176 | std::unique_ptr<RedundancyPolicy> |
| 177 | getAnyof(const fanPolicy& fan, std::unique_ptr<EEPROMDevice> eepromDevice); |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 178 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 179 | /** |
| 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 Spinler | bc4179e | 2022-10-04 15:15:06 -0500 | [diff] [blame] | 184 | * @param[in] eepromDevice - EEPROM device object |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 185 | * |
| 186 | * @return - A `Fallback` redundancy policy |
| 187 | */ |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 188 | std::unique_ptr<RedundancyPolicy> getFallback( |
| 189 | const fanPolicy& fan, std::unique_ptr<EEPROMDevice> eepromDevice); |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 190 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 191 | } // namespace rpolicy |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 192 | |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 193 | } // namespace presence |
| 194 | } // namespace fan |
| 195 | } // namespace phosphor |