control: Add request target base action

Add the YAML based set_request_speed_base_with_max action function as an
action class for JSON configs to use. This action required to be
enhanced to handle the different supported data types of groups that
could be configured in the JSON with this action.

Change-Id: I8cc223cc10d33462ddc303145fee08347ea7c8b5
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/control/Makefile.am b/control/Makefile.am
index d1b246b..c1df84b 100644
--- a/control/Makefile.am
+++ b/control/Makefile.am
@@ -31,7 +31,8 @@
 	json/zone.cpp \
 	json/group.cpp \
 	json/event.cpp \
-	json/actions/default_floor.cpp
+	json/actions/default_floor.cpp \
+	json/actions/request_target_base.cpp
 else
 phosphor_fan_control_SOURCES += \
 	argument.cpp \
diff --git a/control/json/actions/request_target_base.cpp b/control/json/actions/request_target_base.cpp
new file mode 100644
index 0000000..2de3e46
--- /dev/null
+++ b/control/json/actions/request_target_base.cpp
@@ -0,0 +1,92 @@
+/**
+ * Copyright © 2021 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 "request_target_base.hpp"
+
+#include "../manager.hpp"
+#include "../zone.hpp"
+#include "group.hpp"
+
+#include <fmt/format.h>
+
+#include <nlohmann/json.hpp>
+#include <phosphor-logging/log.hpp>
+
+#include <algorithm>
+
+namespace phosphor::fan::control::json
+{
+
+using json = nlohmann::json;
+using namespace phosphor::logging;
+
+RequestTargetBase::RequestTargetBase(const json&) :
+    ActionBase(RequestTargetBase::name)
+{
+    // There are no JSON configuration parameters for this action
+}
+
+void RequestTargetBase::run(Zone& zone, const Group& group)
+{
+    uint64_t base = 0;
+    for (const auto& member : group.getMembers())
+    {
+        try
+        {
+            auto value = Manager::getObjValueVariant(
+                member, group.getInterface(), group.getProperty());
+            if (auto intPtr = std::get_if<int64_t>(&value))
+            {
+                // Throw out any negative values as those are not valid
+                // to use as a fan target base
+                if (*intPtr < 0)
+                {
+                    continue;
+                }
+                base = std::max(base, static_cast<uint64_t>(*intPtr));
+            }
+            else if (auto dblPtr = std::get_if<double>(&value))
+            {
+                // Throw out any negative values as those are not valid
+                // to use as a fan target base
+                if (*dblPtr < 0)
+                {
+                    continue;
+                }
+                // Precision of a double not a concern with fan targets
+                base = std::max(base, static_cast<uint64_t>(*dblPtr));
+            }
+            else
+            {
+                // Unsupported group member type for this action
+                log<level::ERR>(
+                    fmt::format("Action {}: Unsupported group member type "
+                                "given. [object = {} : {} : {}]",
+                                getName(), member, group.getInterface(),
+                                group.getProperty())
+                        .c_str());
+            }
+        }
+        catch (const std::out_of_range& oore)
+        {
+            // Property value not found, base request target unchanged
+        }
+    }
+
+    // A request target base of 0 defaults to the current target
+    zone.setRequestTargetBase(base);
+}
+
+} // namespace phosphor::fan::control::json
diff --git a/control/json/actions/request_target_base.hpp b/control/json/actions/request_target_base.hpp
new file mode 100644
index 0000000..6285dc5
--- /dev/null
+++ b/control/json/actions/request_target_base.hpp
@@ -0,0 +1,75 @@
+/**
+ * Copyright © 2021 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 "../zone.hpp"
+#include "action.hpp"
+#include "group.hpp"
+
+#include <nlohmann/json.hpp>
+
+namespace phosphor::fan::control::json
+{
+
+using json = nlohmann::json;
+
+/**
+ * @class RequestTargetBase - Action to set the requested target base
+ *
+ * Sets the base of a calculated requested target to the maximum value found
+ * from the properties given within a group. The requested target base is what
+ * the calculated requested target should be determined from when changing fan
+ * targets. By default, the base of the next calculated requested target is the
+ * current target of the zone. This action allows that base to be changed
+ * according to the maximum value found from a given group of dbus objects.
+ */
+class RequestTargetBase :
+    public ActionBase,
+    public ActionRegister<RequestTargetBase>
+{
+  public:
+    /* Name of this action */
+    static constexpr auto name = "set_request_speed_base_with_max";
+
+    RequestTargetBase() = delete;
+    RequestTargetBase(const RequestTargetBase&) = delete;
+    RequestTargetBase(RequestTargetBase&&) = delete;
+    RequestTargetBase& operator=(const RequestTargetBase&) = delete;
+    RequestTargetBase& operator=(RequestTargetBase&&) = delete;
+    ~RequestTargetBase() = default;
+
+    /**
+     * @brief Update the requested target base
+     *
+     * No JSON configuration parameters required
+     */
+    explicit RequestTargetBase(const json&);
+
+    /**
+     * @brief Run the action
+     *
+     * Determines the maximum value from the properties of the group of dbus
+     * objects and sets the requested target base to this value. Only positive
+     * integer or floating point types are supported as these are the only
+     * valid types for a fan target to be based off of.
+     *
+     * @param[in] zone - Zone to run the action on
+     * @param[in] group - Group of dbus objects the action runs against
+     */
+    void run(Zone& zone, const Group& group) override;
+};
+
+} // namespace phosphor::fan::control::json
diff --git a/control/json/manager.cpp b/control/json/manager.cpp
index 0cbb9d7..3508307 100644
--- a/control/json/manager.cpp
+++ b/control/json/manager.cpp
@@ -39,6 +39,9 @@
 std::map<std::string,
          std::map<std::pair<std::string, bool>, std::vector<std::string>>>
     Manager::_servTree;
+std::map<std::string,
+         std::map<std::string, std::map<std::string, PropertyVariantType>>>
+    Manager::_objects;
 
 Manager::Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event) :
     _bus(bus), _event(event)
diff --git a/control/json/manager.hpp b/control/json/manager.hpp
index 8919b63..eeedfd3 100644
--- a/control/json/manager.hpp
+++ b/control/json/manager.hpp
@@ -146,6 +146,22 @@
     static bool hasOwner(const std::string& path, const std::string& intf);
 
     /**
+     * @brief Get the object's property value as a variant
+     *
+     * @param[in] path - Path of the object containing the property
+     * @param[in] intf - Interface name containing the property
+     * @param[in] prop - Name of property
+     *
+     * @return - The object's property value as a variant
+     */
+    static inline auto getObjValueVariant(const std::string& path,
+                                          const std::string& intf,
+                                          const std::string& prop)
+    {
+        return _objects.at(path).at(intf).at(prop);
+    };
+
+    /**
      * @brief Get the configured power on delay(OPTIONAL)
      *
      * @return Power on delay in seconds
@@ -181,6 +197,12 @@
                                           std::vector<std::string>>>
         _servTree;
 
+    /* Object map of paths to interfaces of properties and their values */
+    static std::map<
+        std::string,
+        std::map<std::string, std::map<std::string, PropertyVariantType>>>
+        _objects;
+
     /* List of zones configured */
     std::map<configKey, std::unique_ptr<Zone>> _zones;
 
diff --git a/control/json/zone.hpp b/control/json/zone.hpp
index 56fb855..8bf132c 100644
--- a/control/json/zone.hpp
+++ b/control/json/zone.hpp
@@ -189,6 +189,17 @@
     void requestIncrease(uint64_t targetDelta);
 
     /**
+     * @brief Set the requested target base to be used as the target to base a
+     * new requested target from
+     *
+     * @param[in] targetBase - Base target value to use
+     */
+    inline void setRequestTargetBase(uint64_t targetBase)
+    {
+        _requestTargetBase = targetBase;
+    };
+
+    /**
      * @brief Set a property to be persisted
      *
      * @param[in] intf - Interface containing property