monitor:SIGHUP: Create and use system object
Use a system object to handle retrieving the trust groups and fan
definitions configured. This is necessary for handling HUP signals in
the future where a reload of the JSON configuration is done.
Tested:
No change in the loading of the trust groups configuration
No change in the loading of the fan definitions configured
Change-Id: I5df2d54641f80778bbf09d7b1f4588a458e11c71
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/monitor/Makefile.am b/monitor/Makefile.am
index 185c2e3..6c3c435 100644
--- a/monitor/Makefile.am
+++ b/monitor/Makefile.am
@@ -9,7 +9,8 @@
fan.cpp \
main.cpp \
tach_sensor.cpp \
- conditions.cpp
+ conditions.cpp \
+ system.cpp
phosphor_fan_monitor_LDADD = \
$(SDBUSPLUS_LIBS) \
diff --git a/monitor/main.cpp b/monitor/main.cpp
index 84f2fcc..7517d6d 100644
--- a/monitor/main.cpp
+++ b/monitor/main.cpp
@@ -13,15 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
-#include "config.h"
-
#include "argument.hpp"
#include "fan.hpp"
-#ifdef MONITOR_USE_JSON
-#include "json_parser.hpp"
-#endif
-#include "fan_defs.hpp"
+#include "system.hpp"
#include "trust_manager.hpp"
#include <phosphor-logging/log.hpp>
@@ -35,7 +29,6 @@
{
auto event = sdeventplus::Event::get_default();
auto bus = sdbusplus::bus::new_default();
- std::vector<std::unique_ptr<Fan>> fans;
phosphor::fan::util::ArgumentParser args(argc, argv);
if (argc != 2)
@@ -63,49 +56,7 @@
// handle both sd_events (for the timers) and dbus signals.
bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
-#ifdef MONITOR_USE_JSON
- // Get JSON object from monitor JSON config file
- const auto& jsonObj = getJsonObj(bus);
-
- // Retrieve and set trust groups within the trust manager
- auto trust =
- std::make_unique<phosphor::fan::trust::Manager>(getTrustGrps(jsonObj));
-
- // Retrieve fan definitions and create fan objects to be monitored
- for (const auto& fanDef : getFanDefs(jsonObj))
- {
- // Check if a condition exists on the fan
- auto condition = std::get<conditionField>(fanDef);
- if (condition)
- {
- // Condition exists, skip adding fan if it fails
- if (!(*condition)(bus))
- {
- continue;
- }
- }
- fans.emplace_back(
- std::make_unique<Fan>(mode, bus, event, trust, fanDef));
- }
-#else
- auto trust = std::make_unique<phosphor::fan::trust::Manager>(trustGroups);
-
- for (const auto& fanDef : fanDefinitions)
- {
- // Check if a condition exists on the fan
- auto condition = std::get<conditionField>(fanDef);
- if (condition)
- {
- // Condition exists, skip adding fan if it fails
- if (!(*condition)(bus))
- {
- continue;
- }
- }
- fans.emplace_back(
- std::make_unique<Fan>(mode, bus, event, trust, fanDef));
- }
-#endif
+ System system(mode, bus, event);
if (mode == Mode::init)
{
diff --git a/monitor/system.cpp b/monitor/system.cpp
new file mode 100644
index 0000000..032291f
--- /dev/null
+++ b/monitor/system.cpp
@@ -0,0 +1,87 @@
+/**
+ * Copyright © 2020 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "config.h"
+
+#include "system.hpp"
+
+#include "fan.hpp"
+#include "fan_defs.hpp"
+#include "tach_sensor.hpp"
+#include "trust_manager.hpp"
+#include "types.hpp"
+#ifdef MONITOR_USE_JSON
+#include "json_parser.hpp"
+#endif
+
+#include <nlohmann/json.hpp>
+#include <sdbusplus/bus.hpp>
+#include <sdeventplus/event.hpp>
+
+namespace phosphor::fan::monitor
+{
+
+using json = nlohmann::json;
+
+System::System(Mode mode, sdbusplus::bus::bus& bus,
+ const sdeventplus::Event& event) :
+ _mode(mode),
+ _bus(bus), _event(event)
+{
+ json jsonObj = json::object();
+#ifdef MONITOR_USE_JSON
+ jsonObj = getJsonObj(bus);
+#endif
+ // Retrieve and set trust groups within the trust manager
+ _trust = std::make_unique<trust::Manager>(getTrustGroups(jsonObj));
+
+ // Retrieve fan definitions and create fan objects to be monitored
+ for (const auto& fanDef : getFanDefinitions(jsonObj))
+ {
+ // Check if a condition exists on the fan
+ auto condition = std::get<conditionField>(fanDef);
+ if (condition)
+ {
+ // Condition exists, skip adding fan if it fails
+ if (!(*condition)(bus))
+ {
+ continue;
+ }
+ }
+ _fans.emplace_back(
+ std::make_unique<Fan>(mode, bus, event, _trust, fanDef));
+ }
+}
+
+const std::vector<CreateGroupFunction>
+ System::getTrustGroups(const json& jsonObj)
+{
+#ifdef MONITOR_USE_JSON
+ return getTrustGrps(jsonObj);
+#else
+ return trustGroups;
+#endif
+}
+
+const std::vector<FanDefinition> System::getFanDefinitions(const json& jsonObj)
+{
+#ifdef MONITOR_USE_JSON
+ return getFanDefs(jsonObj);
+#else
+ return fanDefinitions;
+#endif
+}
+
+} // namespace phosphor::fan::monitor
diff --git a/monitor/system.hpp b/monitor/system.hpp
new file mode 100644
index 0000000..c72f0d5
--- /dev/null
+++ b/monitor/system.hpp
@@ -0,0 +1,92 @@
+/**
+ * Copyright © 2020 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include "fan.hpp"
+#include "tach_sensor.hpp"
+#include "trust_manager.hpp"
+#include "types.hpp"
+
+#include <nlohmann/json.hpp>
+#include <sdbusplus/bus.hpp>
+#include <sdeventplus/event.hpp>
+
+#include <memory>
+#include <optional>
+#include <vector>
+
+namespace phosphor::fan::monitor
+{
+
+using json = nlohmann::json;
+
+class System
+{
+ public:
+ System() = delete;
+ System(const System&) = delete;
+ System(System&&) = delete;
+ System& operator=(const System&) = delete;
+ System& operator=(System&&) = delete;
+ ~System() = default;
+
+ /**
+ * Constructor
+ * Parses and populates the fan monitor trust groups and list of fans
+ *
+ * @param[in] mode - mode of fan monitor
+ * @param[in] bus - sdbusplus bus object
+ * @param[in] event - event loop reference
+ */
+ System(Mode mode, sdbusplus::bus::bus& bus,
+ const sdeventplus::Event& event);
+
+ private:
+ /* The mode of fan monitor */
+ Mode _mode;
+
+ /* The sdbusplus bus object */
+ sdbusplus::bus::bus& _bus;
+
+ /* The event loop reference */
+ const sdeventplus::Event& _event;
+
+ /* Trust manager of trust groups */
+ std::unique_ptr<phosphor::fan::trust::Manager> _trust;
+
+ /* List of fan objects to monitor */
+ std::vector<std::unique_ptr<Fan>> _fans;
+
+ /**
+ * @brief Retrieve the configured trust groups
+ *
+ * @param[in] jsonObj - JSON object to parse from
+ *
+ * @return List of functions applied on trust groups
+ */
+ const std::vector<CreateGroupFunction> getTrustGroups(const json& jsonObj);
+
+ /**
+ * @brief Retrieve the configured fan definitions
+ *
+ * @param[in] jsonObj - JSON object to parse from
+ *
+ * @return List of fan definition data on the fans configured
+ */
+ const std::vector<FanDefinition> getFanDefinitions(const json& jsonObj);
+};
+
+} // namespace phosphor::fan::monitor