| Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 1 | #pragma once | 
 | 2 |  | 
| Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 3 | #include "fan.hpp" | 
| Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 4 | #include "psensor.hpp" | 
| Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 5 | #include "rpolicy.hpp" | 
 | 6 |  | 
 | 7 | #include <nlohmann/json.hpp> | 
 | 8 | #include <sdbusplus/bus.hpp> | 
 | 9 | #include <sdeventplus/source/signal.hpp> | 
 | 10 |  | 
 | 11 | #include <filesystem> | 
 | 12 | #include <memory> | 
 | 13 | #include <string> | 
 | 14 | #include <vector> | 
| 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"; | 
| Matthew Barth | 9078131 | 2020-05-04 13:08:31 -0500 | [diff] [blame] | 29 | constexpr auto confDbusPath = "/xyz/openbmc_project/inventory/system/chassis"; | 
 | 30 | constexpr auto confDbusIntf = | 
 | 31 |     "xyz.openbmc_project.Inventory.Decorator.Compatible"; | 
 | 32 | constexpr auto confDbusProp = "Names"; | 
| Matthew Barth | f6d7f61 | 2019-12-10 15:22:54 -0600 | [diff] [blame] | 33 |  | 
| Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 34 | using policies = std::vector<std::unique_ptr<RedundancyPolicy>>; | 
| Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 35 |  | 
 | 36 | constexpr auto fanPolicyFanPos = 0; | 
 | 37 | constexpr auto fanPolicySensorListPos = 1; | 
 | 38 | using fanPolicy = std::tuple<Fan, std::vector<std::unique_ptr<PresenceSensor>>>; | 
 | 39 |  | 
| Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 40 | // Presence method handler function | 
| Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 41 | using methodHandler = | 
 | 42 |     std::function<std::unique_ptr<PresenceSensor>(size_t, const json&)>; | 
| Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 43 | // Presence redundancy policy handler function | 
| Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 44 | using rpolicyHandler = | 
 | 45 |     std::function<std::unique_ptr<RedundancyPolicy>(const fanPolicy&)>; | 
| Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 46 |  | 
 | 47 | class JsonConfig | 
 | 48 | { | 
| Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 49 |   public: | 
 | 50 |     JsonConfig() = delete; | 
 | 51 |     JsonConfig(const JsonConfig&) = delete; | 
 | 52 |     JsonConfig(JsonConfig&&) = delete; | 
 | 53 |     JsonConfig& operator=(const JsonConfig&) = delete; | 
 | 54 |     JsonConfig& operator=(JsonConfig&&) = delete; | 
 | 55 |     ~JsonConfig() = default; | 
| Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 56 |  | 
| Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 57 |     /** | 
 | 58 |      * Constructor | 
 | 59 |      * Parses and populates the fan presence policies from a json file | 
 | 60 |      * | 
 | 61 |      * @param[in] bus - sdbusplus bus object | 
 | 62 |      */ | 
 | 63 |     explicit JsonConfig(sdbusplus::bus::bus& bus); | 
| Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 64 |  | 
| Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 65 |     /** | 
 | 66 |      * @brief Get the json config based fan presence policies | 
 | 67 |      * | 
 | 68 |      * @return - The fan presence policies | 
 | 69 |      */ | 
 | 70 |     static const policies& get(); | 
| Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 71 |  | 
| Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 72 |     /** | 
 | 73 |      * @brief Callback function to handle receiving a HUP signal to | 
 | 74 |      * reload the json configuration. | 
 | 75 |      * | 
 | 76 |      * @param[in] sigSrc - sd_event_source signal wrapper | 
 | 77 |      * @param[in] sigInfo - signal info on signal fd | 
 | 78 |      */ | 
 | 79 |     void sighupHandler(sdeventplus::source::Signal& sigSrc, | 
 | 80 |                        const struct signalfd_siginfo* sigInfo); | 
| Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 81 |  | 
| Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 82 |   private: | 
 | 83 |     /* Fan presence policies */ | 
 | 84 |     static policies _policies; | 
| Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 85 |  | 
| Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 86 |     /* The sdbusplus bus object */ | 
 | 87 |     sdbusplus::bus::bus& _bus; | 
| Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 88 |  | 
| Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 89 |     /* Config file to be used */ | 
 | 90 |     fs::path _confFile; | 
| Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 91 |  | 
| Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 92 |     /* List of Fan objects to have presence policies */ | 
 | 93 |     std::vector<fanPolicy> _fans; | 
| Matthew Barth | 5060b10 | 2019-12-16 10:46:35 -0600 | [diff] [blame] | 94 |  | 
| Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 95 |     /* Presence methods mapping to their associated handler function */ | 
 | 96 |     static const std::map<std::string, methodHandler> _methods; | 
| Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 97 |  | 
| Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [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 | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 103 |  | 
| Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 104 |     /** | 
 | 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. | 
| Matthew Barth | 37b2110 | 2020-05-05 10:37:15 -0500 | [diff] [blame] | 107 |      * 1.) From the confOverridePath location | 
 | 108 |      * 2.) From config file found using property value(s) as a relative | 
 | 109 |      * path extension on the base path from the dbus object where: | 
 | 110 |      *     path = Path set in confDbusPath | 
| Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 111 |      *     interface = Interface set in confDbusIntf | 
 | 112 |      *     property = Property set in confDbusProp | 
| Matthew Barth | 37b2110 | 2020-05-05 10:37:15 -0500 | [diff] [blame] | 113 |      * 3.) *DEFAULT* - From the confBasePath location | 
| Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 114 |      */ | 
 | 115 |     const fs::path getConfFile(); | 
| Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 116 |  | 
| Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 117 |     /** | 
 | 118 |      * @brief Load the json config file | 
 | 119 |      */ | 
 | 120 |     void load(); | 
| Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 121 |  | 
| Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 122 |     /** | 
 | 123 |      * @brief Process the json config to extract the defined fan presence | 
 | 124 |      * policies. | 
 | 125 |      * | 
 | 126 |      * @param[in] jsonConf - parsed json configuration data | 
 | 127 |      */ | 
 | 128 |     void process(const json& jsonConf); | 
| Matthew Barth | 5060b10 | 2019-12-16 10:46:35 -0600 | [diff] [blame] | 129 |  | 
| Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 130 |     /** | 
 | 131 |      * @brief Get the redundancy policy of presence detection for a fan | 
 | 132 |      * | 
 | 133 |      * @param[in] rpolicy - policy type to construct | 
 | 134 |      * @param[in] fpolicy - fan policy object | 
 | 135 |      * | 
 | 136 |      * @return - The constructed redundancy policy type for the fan | 
 | 137 |      */ | 
 | 138 |     std::unique_ptr<RedundancyPolicy> getPolicy(const json& rpolicy, | 
 | 139 |                                                 const fanPolicy& fpolicy); | 
| Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 140 | }; | 
 | 141 |  | 
| Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 142 | /** | 
 | 143 |  * Methods of fan presence detection function declarations | 
 | 144 |  */ | 
 | 145 | namespace method | 
 | 146 | { | 
| Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 147 | /** | 
 | 148 |  * @brief Fan presence detection method by tach feedback | 
 | 149 |  * | 
 | 150 |  * @param[in] fanIndex - fan object index to add tach method | 
 | 151 |  * @param[in] method - json properties for a tach method | 
 | 152 |  * | 
 | 153 |  * @return - A presence sensor to detect fan presence by tach feedback | 
 | 154 |  */ | 
 | 155 | std::unique_ptr<PresenceSensor> getTach(size_t fanIndex, const json& method); | 
| Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 156 |  | 
| Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 157 | /** | 
 | 158 |  * @brief Fan presence detection method by gpio | 
 | 159 |  * | 
 | 160 |  * @param[in] fanIndex - fan object index to add gpio method | 
 | 161 |  * @param[in] method - json properties for a gpio method | 
 | 162 |  * | 
 | 163 |  * @return - A presence sensor to detect fan presence by gpio | 
 | 164 |  */ | 
 | 165 | std::unique_ptr<PresenceSensor> getGpio(size_t fanIndex, const json& method); | 
| Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 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 | { | 
| Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 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); | 
| Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 183 |  | 
| Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 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); | 
| Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 193 |  | 
| Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 194 | } // namespace rpolicy | 
| Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 195 |  | 
| Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 196 | } // namespace presence | 
 | 197 | } // namespace fan | 
 | 198 | } // namespace phosphor |