control: Zone configuration framework and basic attrs
The zone class contains the configuration attributes for each zone
within a system. Each zone is a logical grouping of fans as configured
within the `fans.json` file.
Tested:
Parsed each basic attribute from `zones.json` file
Zone objects created per profile configured
Change-Id: I85f52661831ec201a1b0ee1358c239dbc22c8e49
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/control/json/Makefile.am b/control/json/Makefile.am
index 7d16d6f..a367767 100644
--- a/control/json/Makefile.am
+++ b/control/json/Makefile.am
@@ -14,4 +14,5 @@
libfan_control_json_la_SOURCES = \
manager.cpp \
profile.cpp \
- fan.cpp
+ fan.cpp \
+ zone.cpp
diff --git a/control/json/zone.cpp b/control/json/zone.cpp
new file mode 100644
index 0000000..24dceee
--- /dev/null
+++ b/control/json/zone.cpp
@@ -0,0 +1,81 @@
+/**
+ * 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 "zone.hpp"
+
+#include <nlohmann/json.hpp>
+#include <phosphor-logging/log.hpp>
+#include <sdbusplus/bus.hpp>
+
+namespace phosphor::fan::control::json
+{
+
+using json = nlohmann::json;
+using namespace phosphor::logging;
+
+Zone::Zone(sdbusplus::bus::bus& bus, const json& jsonObj) :
+ ConfigBase(jsonObj), _incDelay(0)
+{
+ if (jsonObj.contains("profiles"))
+ {
+ for (const auto& profile : jsonObj["profiles"])
+ {
+ _profiles.emplace_back(profile.get<std::string>());
+ }
+ }
+ // Speed increase delay is optional, defaults to 0
+ if (jsonObj.contains("increase_delay"))
+ {
+ _incDelay = jsonObj["increase_delay"].get<uint64_t>();
+ }
+ setFullSpeed(jsonObj);
+ setDefaultFloor(jsonObj);
+ setDecInterval(jsonObj);
+}
+
+void Zone::setFullSpeed(const json& jsonObj)
+{
+ if (!jsonObj.contains("full_speed"))
+ {
+ log<level::ERR>("Missing required zone's full speed",
+ entry("JSON=%s", jsonObj.dump().c_str()));
+ throw std::runtime_error("Missing required zone's full speed");
+ }
+ _fullSpeed = jsonObj["full_speed"].get<uint64_t>();
+}
+
+void Zone::setDefaultFloor(const json& jsonObj)
+{
+ if (!jsonObj.contains("default_floor"))
+ {
+ log<level::ERR>("Missing required zone's default floor speed",
+ entry("JSON=%s", jsonObj.dump().c_str()));
+ throw std::runtime_error("Missing required zone's default floor speed");
+ }
+ _defaultFloor = jsonObj["default_floor"].get<uint64_t>();
+}
+
+void Zone::setDecInterval(const json& jsonObj)
+{
+ if (!jsonObj.contains("decrease_interval"))
+ {
+ log<level::ERR>("Missing required zone's decrease interval",
+ entry("JSON=%s", jsonObj.dump().c_str()));
+ throw std::runtime_error("Missing required zone's decrease interval");
+ }
+ _decInterval = jsonObj["decrease_interval"].get<uint64_t>();
+}
+
+} // namespace phosphor::fan::control::json
diff --git a/control/json/zone.hpp b/control/json/zone.hpp
new file mode 100644
index 0000000..99c8fa5
--- /dev/null
+++ b/control/json/zone.hpp
@@ -0,0 +1,166 @@
+/**
+ * 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 "config_base.hpp"
+
+#include <nlohmann/json.hpp>
+#include <sdbusplus/bus.hpp>
+
+namespace phosphor::fan::control::json
+{
+
+using json = nlohmann::json;
+
+/**
+ * @class Zone - Represents a configured fan control zone
+ *
+ * A zone object contains the configured attributes for a zone that groups
+ * a number of fans together to be under the same speed control. These
+ * configuration attributes include, but are not limited to, the full speed
+ * of the fans within the zone, a default floor speed, the delay between speed
+ * increases, a decrease interval, and any profiles(OPTIONAL) the zone should
+ * be included in.
+ *
+ * (When no profile for a zone is given, the zone defaults to always exist)
+ *
+ */
+class Zone : public ConfigBase
+{
+ public:
+ /* JSON file name for zones */
+ static constexpr auto confFileName = "zones.json";
+
+ Zone() = delete;
+ Zone(const Zone&) = delete;
+ Zone(Zone&&) = delete;
+ Zone& operator=(const Zone&) = delete;
+ Zone& operator=(Zone&&) = delete;
+ ~Zone() = default;
+
+ /**
+ * Constructor
+ * Parses and populates a zone from JSON object data
+ *
+ * @param[in] bus - sdbusplus bus object
+ * @param[in] jsonObj - JSON object
+ */
+ Zone(sdbusplus::bus::bus& bus, const json& jsonObj);
+
+ /**
+ * @brief Get the full speed
+ *
+ * Full speed is the speed set to the fans within this zone unless there
+ * are events configured that alter the fan speeds.
+ *
+ * @return Full speed of this zone
+ */
+ inline const auto& getFullSpeed() const
+ {
+ return _fullSpeed;
+ }
+
+ /**
+ * @brief Get the default floor speed
+ *
+ * The default floor speed is the lowest speed the fans within this zone
+ * are allowed to decrease to. The zone's floor speed defaults to this
+ * unless changed by some configured event.
+ *
+ * @return Default floor speed
+ */
+ inline const auto& getDefaultFloor() const
+ {
+ return _defaultFloor;
+ }
+
+ /**
+ * @brief Get the speed increase delay(OPTIONAL)
+ *
+ * The speed increase delay is the amount of time(in seconds) increases
+ * to a target speed are delayed before being made. The default is 0, which
+ * results in immediate speed increase requests when any events result in
+ * a change to the target speed.
+ *
+ * It is recommend a value other than 0 is configured, but that inherently
+ * depends on the fan controller and configured speed increases.
+ *
+ * @return Speed increase delay(in seconds)
+ */
+ inline const auto& getIncDelay() const
+ {
+ return _incDelay;
+ }
+
+ /**
+ * @brief Get the speed decrease interval
+ *
+ * Speed decreases happen on a set interval when no requests for an increase
+ * in fan speeds exists. This is the interval(in seconds) at which the fans
+ * within the zone are decreased if events exist that result in a target
+ * speed decrease.
+ *
+ * @return Speed decrease interval(in seconds)
+ */
+ inline const auto& getDecInterval() const
+ {
+ return _decInterval;
+ }
+
+ private:
+ /* The zone's full speed value for fans */
+ uint64_t _fullSpeed;
+
+ /* The zone's default floor speed value for fans */
+ uint64_t _defaultFloor;
+
+ /* Zone's speed increase delay(in seconds) (OPTIONAL) */
+ uint64_t _incDelay;
+
+ /* Zone's speed decrease interval(in seconds) */
+ uint64_t _decInterval;
+
+ /**
+ * @brief Parse and set the zone's full speed value
+ *
+ * @param[in] jsonObj - JSON object for the zone
+ *
+ * Sets the full speed value for the zone from the JSON configuration object
+ */
+ void setFullSpeed(const json& jsonObj);
+
+ /**
+ * @brief Parse and set the zone's default floor speed value
+ *
+ * @param[in] jsonObj - JSON object for the zone
+ *
+ * Sets the default floor speed value for the zone from the JSON
+ * configuration object
+ */
+ void setDefaultFloor(const json& jsonObj);
+
+ /**
+ * @brief Parse and set the zone's decrease interval(in seconds)
+ *
+ * @param[in] jsonObj - JSON object for the zone
+ *
+ * Sets the speed decrease interval(in seconds) for the zone from the JSON
+ * configuration object
+ */
+ void setDecInterval(const json& jsonObj);
+};
+
+} // namespace phosphor::fan::control::json
diff --git a/control/json_parser.cpp b/control/json_parser.cpp
index 5e43de0..65773c1 100644
--- a/control/json_parser.cpp
+++ b/control/json_parser.cpp
@@ -18,6 +18,7 @@
#include "json/fan.hpp"
#include "json/manager.hpp"
#include "json/profile.hpp"
+#include "json/zone.hpp"
#include "types.hpp"
#include <sdbusplus/bus.hpp>
@@ -33,6 +34,8 @@
auto profiles = getConfig<json::Profile>(bus, true);
// Fans to be controlled
auto fans = getConfig<json::Fan>(bus);
+ // Zones within the system
+ auto zones = getConfig<json::Zone>(bus);
// TODO Create zone groups after loading all JSON config files