presence: Use shared JSON config file finding & loading header
Use the shared JSON config determination header for loading its
JSON configuration file.
Tested:
run phosphor-fan-presence-tach can find and load config.json from
/usr/share/phosphor-fan-presence/presence and
/etc/phosphor-fan-presence/presence/ in witherspoon qemu
Change-Id: I5c4fdf7d96de7c956ca24b4a5751911cc1bf1f03
Signed-off-by: Jolie Ku <jolie_ku@wistron.com>
diff --git a/presence/json_parser.hpp b/presence/json_parser.hpp
new file mode 100644
index 0000000..9d99a40
--- /dev/null
+++ b/presence/json_parser.hpp
@@ -0,0 +1,172 @@
+#pragma once
+
+#include "fan.hpp"
+#include "psensor.hpp"
+#include "rpolicy.hpp"
+
+#include <nlohmann/json.hpp>
+#include <sdbusplus/bus.hpp>
+#include <sdeventplus/source/signal.hpp>
+
+#include <filesystem>
+#include <memory>
+#include <string>
+#include <vector>
+
+namespace phosphor
+{
+namespace fan
+{
+namespace presence
+{
+
+namespace fs = std::filesystem;
+using json = nlohmann::json;
+
+constexpr auto confFileName = "config.json";
+constexpr auto confAppName = "presence";
+
+using policies = std::vector<std::unique_ptr<RedundancyPolicy>>;
+
+constexpr auto fanPolicyFanPos = 0;
+constexpr auto fanPolicySensorListPos = 1;
+using fanPolicy = std::tuple<Fan, std::vector<std::unique_ptr<PresenceSensor>>>;
+
+// Presence method handler function
+using methodHandler =
+ std::function<std::unique_ptr<PresenceSensor>(size_t, const json&)>;
+// Presence redundancy policy handler function
+using rpolicyHandler =
+ std::function<std::unique_ptr<RedundancyPolicy>(const fanPolicy&)>;
+
+class JsonConfig
+{
+ public:
+ JsonConfig() = delete;
+ JsonConfig(const JsonConfig&) = delete;
+ JsonConfig(JsonConfig&&) = delete;
+ JsonConfig& operator=(const JsonConfig&) = delete;
+ JsonConfig& operator=(JsonConfig&&) = delete;
+ ~JsonConfig() = default;
+
+ /**
+ * Constructor
+ * Parses and populates the fan presence policies from a json file
+ *
+ * @param[in] bus - sdbusplus bus object
+ */
+ explicit JsonConfig(sdbusplus::bus::bus& bus);
+
+ /**
+ * @brief Get the json config based fan presence policies
+ *
+ * @return - The fan presence policies
+ */
+ static const policies& get();
+
+ /**
+ * @brief Callback function to handle receiving a HUP signal to
+ * reload the json configuration.
+ *
+ * @param[in] sigSrc - sd_event_source signal wrapper
+ * @param[in] sigInfo - signal info on signal fd
+ */
+ void sighupHandler(sdeventplus::source::Signal& sigSrc,
+ const struct signalfd_siginfo* sigInfo);
+
+ private:
+ /* Fan presence policies */
+ static policies _policies;
+
+ /* The sdbusplus bus object */
+ sdbusplus::bus::bus& _bus;
+
+ /* List of Fan objects to have presence policies */
+ std::vector<fanPolicy> _fans;
+
+ /* Presence methods mapping to their associated handler function */
+ static const std::map<std::string, methodHandler> _methods;
+
+ /**
+ * Presence redundancy policy mapping to their associated handler
+ * function
+ */
+ static const std::map<std::string, rpolicyHandler> _rpolicies;
+
+ /**
+ * @brief Process the json config to extract the defined fan presence
+ * policies.
+ *
+ * @param[in] jsonConf - parsed json configuration data
+ */
+ void process(const json& jsonConf);
+
+ /**
+ * @brief Get the redundancy policy of presence detection for a fan
+ *
+ * @param[in] rpolicy - policy type to construct
+ * @param[in] fpolicy - fan policy object
+ *
+ * @return - The constructed redundancy policy type for the fan
+ */
+ std::unique_ptr<RedundancyPolicy> getPolicy(const json& rpolicy,
+ const fanPolicy& fpolicy);
+};
+
+/**
+ * Methods of fan presence detection function declarations
+ */
+namespace method
+{
+/**
+ * @brief Fan presence detection method by tach feedback
+ *
+ * @param[in] fanIndex - fan object index to add tach method
+ * @param[in] method - json properties for a tach method
+ *
+ * @return - A presence sensor to detect fan presence by tach feedback
+ */
+std::unique_ptr<PresenceSensor> getTach(size_t fanIndex, const json& method);
+
+/**
+ * @brief Fan presence detection method by gpio
+ *
+ * @param[in] fanIndex - fan object index to add gpio method
+ * @param[in] method - json properties for a gpio method
+ *
+ * @return - A presence sensor to detect fan presence by gpio
+ */
+std::unique_ptr<PresenceSensor> getGpio(size_t fanIndex, const json& method);
+
+} // namespace method
+
+/**
+ * Redundancy policies for fan presence detection function declarations
+ */
+namespace rpolicy
+{
+/**
+ * @brief Create an `Anyof` redundancy policy on the created presence
+ * sensors for a fan
+ *
+ * @param[in] fan - fan policy object with the presence sensors for the fan
+ *
+ * @return - An `Anyof` redundancy policy
+ */
+std::unique_ptr<RedundancyPolicy> getAnyof(const fanPolicy& fan);
+
+/**
+ * @brief Create a `Fallback` redundancy policy on the created presence
+ * sensors for a fan
+ *
+ * @param[in] fan - fan policy object with the presence sensors for the fan
+ *
+ * @return - A `Fallback` redundancy policy
+ */
+std::unique_ptr<RedundancyPolicy> getFallback(const fanPolicy& fan);
+
+} // namespace rpolicy
+
+} // namespace presence
+} // namespace fan
+} // namespace phosphor