xref: /openbmc/phosphor-power/phosphor-regulators/src/rule.hpp (revision f54021972b91be5058b50e9046bb0dd5a3b22a80)
1a2461b34SShawn McCarney /**
2a2461b34SShawn McCarney  * Copyright © 2019 IBM Corporation
3a2461b34SShawn McCarney  *
4a2461b34SShawn McCarney  * Licensed under the Apache License, Version 2.0 (the "License");
5a2461b34SShawn McCarney  * you may not use this file except in compliance with the License.
6a2461b34SShawn McCarney  * You may obtain a copy of the License at
7a2461b34SShawn McCarney  *
8a2461b34SShawn McCarney  *     http://www.apache.org/licenses/LICENSE-2.0
9a2461b34SShawn McCarney  *
10a2461b34SShawn McCarney  * Unless required by applicable law or agreed to in writing, software
11a2461b34SShawn McCarney  * distributed under the License is distributed on an "AS IS" BASIS,
12a2461b34SShawn McCarney  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a2461b34SShawn McCarney  * See the License for the specific language governing permissions and
14a2461b34SShawn McCarney  * limitations under the License.
15a2461b34SShawn McCarney  */
16a2461b34SShawn McCarney #pragma once
17a2461b34SShawn McCarney 
189fc08e73SShawn McCarney #include "action.hpp"
199fc08e73SShawn McCarney #include "action_environment.hpp"
209fc08e73SShawn McCarney #include "action_utils.hpp"
219fc08e73SShawn McCarney 
229fc08e73SShawn McCarney #include <memory>
23a2461b34SShawn McCarney #include <string>
249fc08e73SShawn McCarney #include <utility>
259fc08e73SShawn McCarney #include <vector>
26a2461b34SShawn McCarney 
27ea7385b8SShawn McCarney namespace phosphor::power::regulators
28a2461b34SShawn McCarney {
29a2461b34SShawn McCarney 
30a2461b34SShawn McCarney /**
31a2461b34SShawn McCarney  * @class Rule
32a2461b34SShawn McCarney  *
33a2461b34SShawn McCarney  * A rule is a sequence of actions that can be shared by multiple voltage
34a2461b34SShawn McCarney  * regulators.
35a2461b34SShawn McCarney  *
36a2461b34SShawn McCarney  * Rules define a standard way to perform an operation.  For example, the
37a2461b34SShawn McCarney  * following action sequences might be sharable using a rule:
38a2461b34SShawn McCarney  * - Actions that set the output voltage of a regulator rail
39a2461b34SShawn McCarney  * - Actions that read all the sensors of a regulator rail
40a2461b34SShawn McCarney  * - Actions that detect down-level hardware using version registers
41a2461b34SShawn McCarney  */
42a2461b34SShawn McCarney class Rule
43a2461b34SShawn McCarney {
44a2461b34SShawn McCarney   public:
45a2461b34SShawn McCarney     // Specify which compiler-generated methods we want
46a2461b34SShawn McCarney     Rule() = delete;
47a2461b34SShawn McCarney     Rule(const Rule&) = delete;
48a2461b34SShawn McCarney     Rule(Rule&&) = delete;
49a2461b34SShawn McCarney     Rule& operator=(const Rule&) = delete;
50a2461b34SShawn McCarney     Rule& operator=(Rule&&) = delete;
51a2461b34SShawn McCarney     ~Rule() = default;
52a2461b34SShawn McCarney 
53a2461b34SShawn McCarney     /**
54a2461b34SShawn McCarney      * Constructor.
55a2461b34SShawn McCarney      *
56a2461b34SShawn McCarney      * @param id unique rule ID
579fc08e73SShawn McCarney      * @param actions actions in the rule
58a2461b34SShawn McCarney      */
Rule(const std::string & id,std::vector<std::unique_ptr<Action>> actions)599fc08e73SShawn McCarney     explicit Rule(const std::string& id,
609fc08e73SShawn McCarney                   std::vector<std::unique_ptr<Action>> actions) :
61*f5402197SPatrick Williams         id{id}, actions{std::move(actions)}
620c9a33d6SAdriana Kobylak     {}
63a2461b34SShawn McCarney 
64a2461b34SShawn McCarney     /**
659fc08e73SShawn McCarney      * Executes the actions in this rule.
669fc08e73SShawn McCarney      *
679fc08e73SShawn McCarney      * Returns the return value from the last action.
689fc08e73SShawn McCarney      *
699fc08e73SShawn McCarney      * Throws an exception if an error occurs and an action cannot be
709fc08e73SShawn McCarney      * successfully executed.
719fc08e73SShawn McCarney      *
729fc08e73SShawn McCarney      * @param environment action execution environment
739fc08e73SShawn McCarney      * @return return value from last action in rule
749fc08e73SShawn McCarney      */
execute(ActionEnvironment & environment)759fc08e73SShawn McCarney     bool execute(ActionEnvironment& environment)
769fc08e73SShawn McCarney     {
779fc08e73SShawn McCarney         return action_utils::execute(actions, environment);
789fc08e73SShawn McCarney     }
799fc08e73SShawn McCarney 
809fc08e73SShawn McCarney     /**
819fc08e73SShawn McCarney      * Returns the actions in this rule.
829fc08e73SShawn McCarney      *
839fc08e73SShawn McCarney      * @return actions in rule
849fc08e73SShawn McCarney      */
getActions() const859fc08e73SShawn McCarney     const std::vector<std::unique_ptr<Action>>& getActions() const
869fc08e73SShawn McCarney     {
879fc08e73SShawn McCarney         return actions;
889fc08e73SShawn McCarney     }
899fc08e73SShawn McCarney 
909fc08e73SShawn McCarney     /**
91a2461b34SShawn McCarney      * Returns the unique ID of this rule.
92a2461b34SShawn McCarney      *
93a2461b34SShawn McCarney      * @return rule ID
94a2461b34SShawn McCarney      */
getID() const954afb285eSShawn McCarney     const std::string& getID() const
96a2461b34SShawn McCarney     {
97a2461b34SShawn McCarney         return id;
98a2461b34SShawn McCarney     }
99a2461b34SShawn McCarney 
100a2461b34SShawn McCarney   private:
101a2461b34SShawn McCarney     /**
102a2461b34SShawn McCarney      * Unique ID of this rule.
103a2461b34SShawn McCarney      */
104a2461b34SShawn McCarney     const std::string id{};
1059fc08e73SShawn McCarney 
1069fc08e73SShawn McCarney     /**
1079fc08e73SShawn McCarney      * Actions in this rule.
1089fc08e73SShawn McCarney      */
1099fc08e73SShawn McCarney     std::vector<std::unique_ptr<Action>> actions{};
110a2461b34SShawn McCarney };
111a2461b34SShawn McCarney 
112ea7385b8SShawn McCarney } // namespace phosphor::power::regulators
113