1 /**
2  * Copyright © 2020 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 "chassis.hpp"
19 #include "id_map.hpp"
20 #include "rule.hpp"
21 #include "services.hpp"
22 
23 #include <memory>
24 #include <utility>
25 #include <vector>
26 
27 namespace phosphor::power::regulators
28 {
29 
30 /**
31  * @class System
32  *
33  * The computer system being controlled and monitored by the BMC.
34  *
35  * The system contains one or more chassis.  Chassis are large enclosures that
36  * can be independently powered off and on by the BMC.
37  */
38 class System
39 {
40   public:
41     // Specify which compiler-generated methods we want
42     System() = delete;
43     System(const System&) = delete;
44     System(System&&) = delete;
45     System& operator=(const System&) = delete;
46     System& operator=(System&&) = delete;
47     ~System() = default;
48 
49     /**
50      * Constructor.
51      *
52      * @param rules rules used to monitor and control regulators in the system
53      * @param chassis chassis in the system
54      */
55     explicit System(std::vector<std::unique_ptr<Rule>> rules,
56                     std::vector<std::unique_ptr<Chassis>> chassis) :
57         rules{std::move(rules)},
58         chassis{std::move(chassis)}
59     {
60         buildIDMap();
61     }
62 
63     /**
64      * Clear any cached data about hardware devices.
65      */
66     void clearCache();
67 
68     /**
69      * Clears all error history.
70      *
71      * All data on previously logged errors will be deleted.  If errors occur
72      * again in the future they will be logged again.
73      *
74      * This method is normally called when the system is being powered on.
75      */
76     void clearErrorHistory();
77 
78     /**
79      * Close the regulator devices in the system.
80      *
81      * @param services system services like error logging and the journal
82      */
83     void closeDevices(Services& services);
84 
85     /**
86      * Configure the regulator devices in the system.
87      *
88      * This method should be called during the boot before regulators are
89      * enabled.
90      *
91      * @param services system services like error logging and the journal
92      */
93     void configure(Services& services);
94 
95     /**
96      * Detect redundant phase faults in regulator devices in the system.
97      *
98      * This method should be called repeatedly based on a timer.
99      *
100      * @param services system services like error logging and the journal
101      */
102     void detectPhaseFaults(Services& services);
103 
104     /**
105      * Returns the chassis in the system.
106      *
107      * @return chassis
108      */
109     const std::vector<std::unique_ptr<Chassis>>& getChassis() const
110     {
111         return chassis;
112     }
113 
114     /**
115      * Returns the IDMap for the system.
116      *
117      * The IDMap provides a mapping from string IDs to the associated Device,
118      * Rail, and Rule objects.
119      *
120      * @return IDMap
121      */
122     const IDMap& getIDMap() const
123     {
124         return idMap;
125     }
126 
127     /**
128      * Returns the rules used to monitor and control regulators in the system.
129      *
130      * @return rules
131      */
132     const std::vector<std::unique_ptr<Rule>>& getRules() const
133     {
134         return rules;
135     }
136 
137     /**
138      * Monitors the sensors for the voltage rails produced by this system, if
139      * any.
140      *
141      * This method should be called repeatedly based on a timer.
142      *
143      * @param services system services like error logging and the journal
144      */
145     void monitorSensors(Services& services);
146 
147   private:
148     /**
149      * Builds the IDMap for the system.
150      *
151      * Adds the Device, Rail, and Rule objects in the system to the map.
152      */
153     void buildIDMap();
154 
155     /**
156      * Rules used to monitor and control regulators in the system.
157      */
158     std::vector<std::unique_ptr<Rule>> rules{};
159 
160     /**
161      * Chassis in the system.
162      */
163     std::vector<std::unique_ptr<Chassis>> chassis{};
164 
165     /**
166      * Mapping from string IDs to the associated Device, Rail, and Rule objects.
167      */
168     IDMap idMap{};
169 };
170 
171 } // namespace phosphor::power::regulators
172