xref: /openbmc/phosphor-fan-presence/presence/json_parser.hpp (revision 4fa67aa12bd73f7d528225f0c3c93c3242bbcc23)
11a568650SJolie Ku #pragma once
21a568650SJolie Ku 
3e8122390SMatt Spinler #include "error_reporter.hpp"
41a568650SJolie Ku #include "fan.hpp"
51a568650SJolie Ku #include "psensor.hpp"
61a568650SJolie Ku #include "rpolicy.hpp"
71a568650SJolie Ku 
81a568650SJolie Ku #include <nlohmann/json.hpp>
91a568650SJolie Ku #include <sdbusplus/bus.hpp>
101a568650SJolie Ku #include <sdeventplus/source/signal.hpp>
111a568650SJolie Ku 
121a568650SJolie Ku #include <filesystem>
131a568650SJolie Ku #include <memory>
141a568650SJolie Ku #include <string>
151a568650SJolie Ku #include <vector>
161a568650SJolie Ku 
171a568650SJolie Ku namespace phosphor
181a568650SJolie Ku {
191a568650SJolie Ku namespace fan
201a568650SJolie Ku {
211a568650SJolie Ku namespace presence
221a568650SJolie Ku {
231a568650SJolie Ku 
241a568650SJolie Ku namespace fs = std::filesystem;
251a568650SJolie Ku using json = nlohmann::json;
261a568650SJolie Ku 
271a568650SJolie Ku constexpr auto confFileName = "config.json";
281a568650SJolie Ku constexpr auto confAppName = "presence";
291a568650SJolie Ku 
301a568650SJolie Ku using policies = std::vector<std::unique_ptr<RedundancyPolicy>>;
311a568650SJolie Ku 
321a568650SJolie Ku constexpr auto fanPolicyFanPos = 0;
331a568650SJolie Ku constexpr auto fanPolicySensorListPos = 1;
341a568650SJolie Ku using fanPolicy = std::tuple<Fan, std::vector<std::unique_ptr<PresenceSensor>>>;
351a568650SJolie Ku 
361a568650SJolie Ku // Presence method handler function
371a568650SJolie Ku using methodHandler =
381a568650SJolie Ku     std::function<std::unique_ptr<PresenceSensor>(size_t, const json&)>;
391a568650SJolie Ku // Presence redundancy policy handler function
40bc4179e9SMatt Spinler using rpolicyHandler = std::function<std::unique_ptr<RedundancyPolicy>(
41bc4179e9SMatt Spinler     const fanPolicy&, std::unique_ptr<EEPROMDevice>)>;
421a568650SJolie Ku 
431a568650SJolie Ku class JsonConfig
441a568650SJolie Ku {
451a568650SJolie Ku   public:
461a568650SJolie Ku     JsonConfig() = delete;
471a568650SJolie Ku     JsonConfig(const JsonConfig&) = delete;
481a568650SJolie Ku     JsonConfig(JsonConfig&&) = delete;
491a568650SJolie Ku     JsonConfig& operator=(const JsonConfig&) = delete;
501a568650SJolie Ku     JsonConfig& operator=(JsonConfig&&) = delete;
511a568650SJolie Ku     ~JsonConfig() = default;
521a568650SJolie Ku 
531a568650SJolie Ku     /**
541a568650SJolie Ku      * Constructor
551a568650SJolie Ku      *
561a568650SJolie Ku      * @param[in] bus - sdbusplus bus object
571a568650SJolie Ku      */
58cb356d48SPatrick Williams     explicit JsonConfig(sdbusplus::bus_t& bus);
591a568650SJolie Ku 
601a568650SJolie Ku     /**
611a568650SJolie Ku      * @brief Get the json config based fan presence policies
621a568650SJolie Ku      *
631a568650SJolie Ku      * @return - The fan presence policies
641a568650SJolie Ku      */
651a568650SJolie Ku     static const policies& get();
661a568650SJolie Ku 
671a568650SJolie Ku     /**
681a568650SJolie Ku      * @brief Callback function to handle receiving a HUP signal to
691a568650SJolie Ku      * reload the json configuration.
701a568650SJolie Ku      *
711a568650SJolie Ku      * @param[in] sigSrc - sd_event_source signal wrapper
721a568650SJolie Ku      * @param[in] sigInfo - signal info on signal fd
731a568650SJolie Ku      */
74808d7fe8SMike Capps     void sighupHandler(sdeventplus::source::Signal& /*sigSrc*/,
75808d7fe8SMike Capps                        const struct signalfd_siginfo* /*sigInfo*/);
761a568650SJolie Ku 
770daedd18SMatt Spinler     /**
780daedd18SMatt Spinler      * @brief Parses and populates the fan presence policies from
790daedd18SMatt Spinler      *        the json file and then starts the actual presence
800daedd18SMatt Spinler      *        detecting.
810daedd18SMatt Spinler      */
825b839919SMatthew Barth     void start();
830daedd18SMatt Spinler 
841a568650SJolie Ku   private:
851a568650SJolie Ku     /* Fan presence policies */
861a568650SJolie Ku     static policies _policies;
871a568650SJolie Ku 
881a568650SJolie Ku     /* The sdbusplus bus object */
89cb356d48SPatrick Williams     sdbusplus::bus_t& _bus;
901a568650SJolie Ku 
911a568650SJolie Ku     /* List of Fan objects to have presence policies */
921a568650SJolie Ku     std::vector<fanPolicy> _fans;
931a568650SJolie Ku 
941a568650SJolie Ku     /* Presence methods mapping to their associated handler function */
951a568650SJolie Ku     static const std::map<std::string, methodHandler> _methods;
961a568650SJolie Ku 
971a568650SJolie Ku     /**
981a568650SJolie Ku      * Presence redundancy policy mapping to their associated handler
991a568650SJolie Ku      * function
1001a568650SJolie Ku      */
1011a568650SJolie Ku     static const std::map<std::string, rpolicyHandler> _rpolicies;
1021a568650SJolie Ku 
1031a568650SJolie Ku     /**
104e8122390SMatt Spinler      * Class that handles reporting errors for missing fans.
105e8122390SMatt Spinler      */
106e8122390SMatt Spinler     std::unique_ptr<ErrorReporter> _reporter;
107e8122390SMatt Spinler 
108e8122390SMatt Spinler     /**
109dfc8c4d0SMatt Spinler      * Tracks if the config has already been loaded.
110dfc8c4d0SMatt Spinler      */
111dfc8c4d0SMatt Spinler     bool _loaded = false;
112dfc8c4d0SMatt Spinler 
113dfc8c4d0SMatt Spinler     /**
1141a568650SJolie Ku      * @brief Process the json config to extract the defined fan presence
1151a568650SJolie Ku      * policies.
1161a568650SJolie Ku      *
1171a568650SJolie Ku      * @param[in] jsonConf - parsed json configuration data
1181a568650SJolie Ku      */
1191a568650SJolie Ku     void process(const json& jsonConf);
1201a568650SJolie Ku 
1211a568650SJolie Ku     /**
1221a568650SJolie Ku      * @brief Get the redundancy policy of presence detection for a fan
1231a568650SJolie Ku      *
1241a568650SJolie Ku      * @param[in] rpolicy - policy type to construct
1251a568650SJolie Ku      * @param[in] fpolicy - fan policy object
126bc4179e9SMatt Spinler      * @param[in] eepromDevice - EEPROM device object
1271a568650SJolie Ku      *
1281a568650SJolie Ku      * @return - The constructed redundancy policy type for the fan
1291a568650SJolie Ku      */
130*4fa67aa1SPatrick Williams     std::unique_ptr<RedundancyPolicy> getPolicy(
131*4fa67aa1SPatrick Williams         const json& rpolicy, const fanPolicy& fpolicy,
132bc4179e9SMatt Spinler         std::unique_ptr<EEPROMDevice> eepromDevice);
1331a568650SJolie Ku };
1341a568650SJolie Ku 
1351a568650SJolie Ku /**
1361a568650SJolie Ku  * Methods of fan presence detection function declarations
1371a568650SJolie Ku  */
1381a568650SJolie Ku namespace method
1391a568650SJolie Ku {
1401a568650SJolie Ku /**
1411a568650SJolie Ku  * @brief Fan presence detection method by tach feedback
1421a568650SJolie Ku  *
1431a568650SJolie Ku  * @param[in] fanIndex - fan object index to add tach method
1441a568650SJolie Ku  * @param[in] method - json properties for a tach method
1451a568650SJolie Ku  *
1461a568650SJolie Ku  * @return - A presence sensor to detect fan presence by tach feedback
1471a568650SJolie Ku  */
1481a568650SJolie Ku std::unique_ptr<PresenceSensor> getTach(size_t fanIndex, const json& method);
1491a568650SJolie Ku 
1501a568650SJolie Ku /**
1511a568650SJolie Ku  * @brief Fan presence detection method by gpio
1521a568650SJolie Ku  *
1531a568650SJolie Ku  * @param[in] fanIndex - fan object index to add gpio method
1541a568650SJolie Ku  * @param[in] method - json properties for a gpio method
1551a568650SJolie Ku  *
1561a568650SJolie Ku  * @return - A presence sensor to detect fan presence by gpio
1571a568650SJolie Ku  */
1581a568650SJolie Ku std::unique_ptr<PresenceSensor> getGpio(size_t fanIndex, const json& method);
1591a568650SJolie Ku 
1601a568650SJolie Ku } // namespace method
1611a568650SJolie Ku 
1621a568650SJolie Ku /**
1631a568650SJolie Ku  * Redundancy policies for fan presence detection function declarations
1641a568650SJolie Ku  */
1651a568650SJolie Ku namespace rpolicy
1661a568650SJolie Ku {
1671a568650SJolie Ku /**
1681a568650SJolie Ku  * @brief Create an `Anyof` redundancy policy on the created presence
1691a568650SJolie Ku  * sensors for a fan
1701a568650SJolie Ku  *
1711a568650SJolie Ku  * @param[in] fan - fan policy object with the presence sensors for the fan
172bc4179e9SMatt Spinler  * @param[in] eepromDevice - EEPROM device object
1731a568650SJolie Ku  *
1741a568650SJolie Ku  * @return - An `Anyof` redundancy policy
1751a568650SJolie Ku  */
176*4fa67aa1SPatrick Williams std::unique_ptr<RedundancyPolicy> getAnyof(
177*4fa67aa1SPatrick Williams     const fanPolicy& fan, std::unique_ptr<EEPROMDevice> eepromDevice);
1781a568650SJolie Ku 
1791a568650SJolie Ku /**
1801a568650SJolie Ku  * @brief Create a `Fallback` redundancy policy on the created presence
1811a568650SJolie Ku  * sensors for a fan
1821a568650SJolie Ku  *
1831a568650SJolie Ku  * @param[in] fan - fan policy object with the presence sensors for the fan
184bc4179e9SMatt Spinler  * @param[in] eepromDevice - EEPROM device object
1851a568650SJolie Ku  *
1861a568650SJolie Ku  * @return - A `Fallback` redundancy policy
1871a568650SJolie Ku  */
188dfddd648SPatrick Williams std::unique_ptr<RedundancyPolicy> getFallback(
189dfddd648SPatrick Williams     const fanPolicy& fan, std::unique_ptr<EEPROMDevice> eepromDevice);
1901a568650SJolie Ku 
1911a568650SJolie Ku } // namespace rpolicy
1921a568650SJolie Ku 
1931a568650SJolie Ku } // namespace presence
1941a568650SJolie Ku } // namespace fan
1951a568650SJolie Ku } // namespace phosphor
196