1 /**
2  * Copyright © 2021 IBM Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include "../utils/modifier.hpp"
19 #include "../zone.hpp"
20 #include "action.hpp"
21 #include "group.hpp"
22 
23 #include <nlohmann/json.hpp>
24 
25 namespace phosphor::fan::control::json
26 {
27 
28 using json = nlohmann::json;
29 
30 /**
31  * @class SetParameterFromGroupMax - Action to store a Parameter based
32  * on the maximum property value of all configured groups.
33  *
34  * Sets a value in the Manager's parameter store based on the maximum
35  * group property value.  The property value can be modified before
36  * storing it if the JSON specifies a valid Modifier class expression.
37  *
38  * For example:
39  *
40  *  {
41  *    "name": "set_parameter_from_group",
42  *    "parameter_name": "proc_0_throttle_temp",
43  *    "modifier": {
44  *      "expression": "subtract",
45  *      "value": 4
46  *    }
47  *  }
48  *
49  * The above JSON will cause the action to read the property specified
50  * by the group, subtract 4 from it, and then write that value to the Manager
51  * using the proc_0_throttle_temp name.
52  *
53  * See the Modifier class documentation for valid expressions.
54  */
55 class SetParameterFromGroupMax :
56     public ActionBase,
57     public ActionRegister<SetParameterFromGroupMax>
58 {
59   public:
60     /* Name of this action */
61     static constexpr auto name = "set_parameter_from_group_max";
62 
63     SetParameterFromGroupMax() = delete;
64     SetParameterFromGroupMax(const SetParameterFromGroupMax&) = delete;
65     SetParameterFromGroupMax(SetParameterFromGroupMax&&) = delete;
66     SetParameterFromGroupMax&
67         operator=(const SetParameterFromGroupMax&) = delete;
68     SetParameterFromGroupMax& operator=(SetParameterFromGroupMax&&) = delete;
69     ~SetParameterFromGroupMax() = default;
70 
71     /**
72      * @brief Constructor
73      *
74      * @param[in] jsonObj - JSON configuration of this action
75      * @param[in] groups - Groups of dbus objects the action uses
76      */
77     SetParameterFromGroupMax(const json& jsonObj,
78                              const std::vector<Group>& groups);
79 
80     /**
81      * @brief Reads a property value from the configured group,
82      *        modifies it if specified, and then store the value
83      *        in the Manager as a parameter.
84      *
85      * @param[in] zone - Zone to run the action on
86      */
87     void run(Zone& zone) override;
88 
89   private:
90     /**
91      * @brief Read the parameter name from the JSON
92      *
93      * @param[in] jsonObj - JSON configuration of this action
94      */
95     void setParameterName(const json& jsonObj);
96 
97     /**
98      * @brief Read the optional modifier from the JSON
99      *
100      * @param[in] jsonObj - JSON configuration of this action
101      */
102     void setModifier(const json& jsonObj);
103 
104     /**
105      * @brief The parameter name
106      */
107     std::string _name;
108 
109     /**
110      * @brief The class used to modify the value
111      *
112      * Only created if a modifier is specified in the JSON.
113      */
114     std::unique_ptr<Modifier> _modifier;
115 };
116 
117 } // namespace phosphor::fan::control::json
118