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 | 5060b10 | 2019-12-16 10:46:35 -0600 | [diff] [blame^] | 9 | #include <sdbusplus/bus.hpp> |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 10 | |
| 11 | #include "config.h" |
| 12 | #include "rpolicy.hpp" |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 13 | #include "fan.hpp" |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 14 | #include "psensor.hpp" |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 15 | |
| 16 | namespace phosphor |
| 17 | { |
| 18 | namespace fan |
| 19 | { |
| 20 | namespace presence |
| 21 | { |
| 22 | |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 23 | namespace fs = std::filesystem; |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 24 | using json = nlohmann::json; |
Matthew Barth | f6d7f61 | 2019-12-10 15:22:54 -0600 | [diff] [blame] | 25 | |
Matthew Barth | 5060b10 | 2019-12-16 10:46:35 -0600 | [diff] [blame^] | 26 | constexpr auto confFileName = "config.json"; |
| 27 | constexpr auto confOverridePath = "/etc/phosphor-fan-presence/presence"; |
| 28 | constexpr auto confBasePath = "/usr/share/phosphor-fan-presence/presence"; |
| 29 | constexpr auto confDbusIntf = "xyz.openbmc_project.Configs.ThermalApps"; |
| 30 | constexpr auto confDbusProp = "FanPresence"; |
Matthew Barth | f6d7f61 | 2019-12-10 15:22:54 -0600 | [diff] [blame] | 31 | |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 32 | using policies = std::vector<std::unique_ptr<RedundancyPolicy>>; |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 33 | |
| 34 | constexpr auto fanPolicyFanPos = 0; |
| 35 | constexpr auto fanPolicySensorListPos = 1; |
| 36 | using fanPolicy = std::tuple<Fan, std::vector<std::unique_ptr<PresenceSensor>>>; |
| 37 | |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 38 | // Presence method handler function |
| 39 | using methodHandler = std::function< |
| 40 | std::unique_ptr<PresenceSensor>(size_t, const json&)>; |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 41 | // Presence redundancy policy handler function |
| 42 | using rpolicyHandler = std::function< |
| 43 | std::unique_ptr<RedundancyPolicy>(const fanPolicy&)>; |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 44 | |
| 45 | class JsonConfig |
| 46 | { |
| 47 | public: |
| 48 | |
| 49 | JsonConfig() = delete; |
| 50 | JsonConfig(const JsonConfig&) = delete; |
| 51 | JsonConfig(JsonConfig&&) = delete; |
| 52 | JsonConfig& operator=(const JsonConfig&) = delete; |
| 53 | JsonConfig& operator=(JsonConfig&&) = delete; |
| 54 | ~JsonConfig() = default; |
| 55 | |
| 56 | /** |
| 57 | * Constructor |
| 58 | * Parses and populates the fan presence policies from a json file |
| 59 | * |
Matthew Barth | 5060b10 | 2019-12-16 10:46:35 -0600 | [diff] [blame^] | 60 | * @param[in] bus - sdbusplus bus object |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 61 | */ |
Matthew Barth | 5060b10 | 2019-12-16 10:46:35 -0600 | [diff] [blame^] | 62 | explicit JsonConfig(sdbusplus::bus::bus& bus); |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * @brief Get the json config based fan presence policies |
| 66 | * |
| 67 | * @return - The fan presence policies |
| 68 | */ |
| 69 | static const policies& get(); |
| 70 | |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 71 | /** |
| 72 | * @brief Callback function to handle receiving a HUP signal to |
| 73 | * reload the json configuration. |
| 74 | * |
| 75 | * @param[in] sigSrc - sd_event_source signal wrapper |
| 76 | * @param[in] sigInfo - signal info on signal fd |
| 77 | */ |
| 78 | void sighupHandler(sdeventplus::source::Signal& sigSrc, |
| 79 | const struct signalfd_siginfo* sigInfo); |
| 80 | |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 81 | private: |
| 82 | |
| 83 | /* Fan presence policies */ |
| 84 | static policies _policies; |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 85 | |
Matthew Barth | 5060b10 | 2019-12-16 10:46:35 -0600 | [diff] [blame^] | 86 | /* The sdbusplus bus object */ |
| 87 | sdbusplus::bus::bus& _bus; |
| 88 | |
| 89 | /* Config file to be used */ |
| 90 | fs::path _confFile; |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 91 | |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 92 | /* List of Fan objects to have presence policies */ |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 93 | std::vector<fanPolicy> _fans; |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 94 | |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 95 | /* Presence methods mapping to their associated handler function */ |
| 96 | static const std::map<std::string, methodHandler> _methods; |
| 97 | |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 98 | /** |
| 99 | * Presence redundancy policy mapping to their associated handler |
| 100 | * function |
| 101 | */ |
| 102 | static const std::map<std::string, rpolicyHandler> _rpolicies; |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 103 | |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 104 | /** |
Matthew Barth | 5060b10 | 2019-12-16 10:46:35 -0600 | [diff] [blame^] | 105 | * Get the json configuration file. The first location found to contain |
| 106 | * the json config file is used from the following locations in order. |
| 107 | * 1.) confOverridePath ("/etc/phosphor-fan-presence/presence") |
| 108 | * 2.) confBasePath ("/usr/share/phosphor-fan-presence/presence") |
| 109 | * 3.) From first dbus object found hosting |
| 110 | * interface = Interface set in confDbusIntf |
| 111 | * property = Property set in confDbusProp |
| 112 | */ |
| 113 | const fs::path getConfFile(); |
| 114 | |
| 115 | /** |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 116 | * @brief Load the json config file |
| 117 | */ |
| 118 | void load(); |
| 119 | |
| 120 | /** |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 121 | * @brief Process the json config to extract the defined fan presence |
| 122 | * policies. |
| 123 | * |
| 124 | * @param[in] jsonConf - parsed json configuration data |
| 125 | */ |
| 126 | void process(const json& jsonConf); |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 127 | |
| 128 | /** |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 129 | * @brief Get the redundancy policy of presence detection for a fan |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 130 | * |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 131 | * @param[in] rpolicy - policy type to construct |
| 132 | * @param[in] fpolicy - fan policy object |
| 133 | * |
| 134 | * @return - The constructed redundancy policy type for the fan |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 135 | */ |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 136 | std::unique_ptr<RedundancyPolicy> getPolicy(const json& rpolicy, |
| 137 | const fanPolicy& fpolicy); |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 138 | }; |
| 139 | |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 140 | /** |
| 141 | * Methods of fan presence detection function declarations |
| 142 | */ |
| 143 | namespace method |
| 144 | { |
| 145 | /** |
| 146 | * @brief Fan presence detection method by tach feedback |
| 147 | * |
| 148 | * @param[in] fanIndex - fan object index to add tach method |
| 149 | * @param[in] method - json properties for a tach method |
| 150 | * |
| 151 | * @return - A presence sensor to detect fan presence by tach feedback |
| 152 | */ |
| 153 | std::unique_ptr<PresenceSensor> getTach(size_t fanIndex, |
| 154 | const json& method); |
| 155 | |
| 156 | /** |
| 157 | * @brief Fan presence detection method by gpio |
| 158 | * |
| 159 | * @param[in] fanIndex - fan object index to add gpio method |
| 160 | * @param[in] method - json properties for a gpio method |
| 161 | * |
| 162 | * @return - A presence sensor to detect fan presence by gpio |
| 163 | */ |
| 164 | std::unique_ptr<PresenceSensor> getGpio(size_t fanIndex, |
| 165 | const json& method); |
| 166 | |
| 167 | } // namespace method |
| 168 | |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 169 | /** |
| 170 | * Redundancy policies for fan presence detection function declarations |
| 171 | */ |
| 172 | namespace rpolicy |
| 173 | { |
| 174 | /** |
| 175 | * @brief Create an `Anyof` redundancy policy on the created presence |
| 176 | * sensors for a fan |
| 177 | * |
| 178 | * @param[in] fan - fan policy object with the presence sensors for the fan |
| 179 | * |
| 180 | * @return - An `Anyof` redundancy policy |
| 181 | */ |
| 182 | std::unique_ptr<RedundancyPolicy> getAnyof(const fanPolicy& fan); |
| 183 | |
| 184 | /** |
| 185 | * @brief Create a `Fallback` redundancy policy on the created presence |
| 186 | * sensors for a fan |
| 187 | * |
| 188 | * @param[in] fan - fan policy object with the presence sensors for the fan |
| 189 | * |
| 190 | * @return - A `Fallback` redundancy policy |
| 191 | */ |
| 192 | std::unique_ptr<RedundancyPolicy> getFallback(const fanPolicy& fan); |
| 193 | |
| 194 | } // namespace policy |
| 195 | |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 196 | } // namespace presence |
| 197 | } // namespace fan |
| 198 | } // namespace phosphor |