| Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 1 | /** | 
|  | 2 | * Copyright © 2019 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 |  | 
| Shawn McCarney | 9fc08e7 | 2019-11-07 13:18:55 -0600 | [diff] [blame] | 18 | #include "action.hpp" | 
|  | 19 | #include "action_environment.hpp" | 
|  | 20 | #include "action_utils.hpp" | 
|  | 21 |  | 
|  | 22 | #include <memory> | 
| Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 23 | #include <string> | 
| Shawn McCarney | 9fc08e7 | 2019-11-07 13:18:55 -0600 | [diff] [blame] | 24 | #include <utility> | 
|  | 25 | #include <vector> | 
| Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 26 |  | 
| Shawn McCarney | ea7385b | 2019-11-07 12:19:32 -0600 | [diff] [blame] | 27 | namespace phosphor::power::regulators | 
| Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 28 | { | 
|  | 29 |  | 
|  | 30 | /** | 
|  | 31 | * @class Rule | 
|  | 32 | * | 
|  | 33 | * A rule is a sequence of actions that can be shared by multiple voltage | 
|  | 34 | * regulators. | 
|  | 35 | * | 
|  | 36 | * Rules define a standard way to perform an operation.  For example, the | 
|  | 37 | * following action sequences might be sharable using a rule: | 
|  | 38 | * - Actions that set the output voltage of a regulator rail | 
|  | 39 | * - Actions that read all the sensors of a regulator rail | 
|  | 40 | * - Actions that detect down-level hardware using version registers | 
|  | 41 | */ | 
|  | 42 | class Rule | 
|  | 43 | { | 
|  | 44 | public: | 
|  | 45 | // Specify which compiler-generated methods we want | 
|  | 46 | Rule() = delete; | 
|  | 47 | Rule(const Rule&) = delete; | 
|  | 48 | Rule(Rule&&) = delete; | 
|  | 49 | Rule& operator=(const Rule&) = delete; | 
|  | 50 | Rule& operator=(Rule&&) = delete; | 
|  | 51 | ~Rule() = default; | 
|  | 52 |  | 
|  | 53 | /** | 
|  | 54 | * Constructor. | 
|  | 55 | * | 
|  | 56 | * @param id unique rule ID | 
| Shawn McCarney | 9fc08e7 | 2019-11-07 13:18:55 -0600 | [diff] [blame] | 57 | * @param actions actions in the rule | 
| Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 58 | */ | 
| Shawn McCarney | 9fc08e7 | 2019-11-07 13:18:55 -0600 | [diff] [blame] | 59 | explicit Rule(const std::string& id, | 
|  | 60 | std::vector<std::unique_ptr<Action>> actions) : | 
|  | 61 | id{id}, | 
|  | 62 | actions{std::move(actions)} | 
| Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 63 | { | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | /** | 
| Shawn McCarney | 9fc08e7 | 2019-11-07 13:18:55 -0600 | [diff] [blame] | 67 | * Executes the actions in this rule. | 
|  | 68 | * | 
|  | 69 | * Returns the return value from the last action. | 
|  | 70 | * | 
|  | 71 | * Throws an exception if an error occurs and an action cannot be | 
|  | 72 | * successfully executed. | 
|  | 73 | * | 
|  | 74 | * @param environment action execution environment | 
|  | 75 | * @return return value from last action in rule | 
|  | 76 | */ | 
|  | 77 | bool execute(ActionEnvironment& environment) | 
|  | 78 | { | 
|  | 79 | return action_utils::execute(actions, environment); | 
|  | 80 | } | 
|  | 81 |  | 
|  | 82 | /** | 
|  | 83 | * Returns the actions in this rule. | 
|  | 84 | * | 
|  | 85 | * @return actions in rule | 
|  | 86 | */ | 
|  | 87 | const std::vector<std::unique_ptr<Action>>& getActions() const | 
|  | 88 | { | 
|  | 89 | return actions; | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | /** | 
| Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 93 | * Returns the unique ID of this rule. | 
|  | 94 | * | 
|  | 95 | * @return rule ID | 
|  | 96 | */ | 
| Shawn McCarney | 4afb285 | 2019-10-27 18:28:53 -0500 | [diff] [blame] | 97 | const std::string& getID() const | 
| Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 98 | { | 
|  | 99 | return id; | 
|  | 100 | } | 
|  | 101 |  | 
|  | 102 | private: | 
|  | 103 | /** | 
|  | 104 | * Unique ID of this rule. | 
|  | 105 | */ | 
|  | 106 | const std::string id{}; | 
| Shawn McCarney | 9fc08e7 | 2019-11-07 13:18:55 -0600 | [diff] [blame] | 107 |  | 
|  | 108 | /** | 
|  | 109 | * Actions in this rule. | 
|  | 110 | */ | 
|  | 111 | std::vector<std::unique_ptr<Action>> actions{}; | 
| Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 112 | }; | 
|  | 113 |  | 
| Shawn McCarney | ea7385b | 2019-11-07 12:19:32 -0600 | [diff] [blame] | 114 | } // namespace phosphor::power::regulators |