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 "device.hpp"
19 #include "id_map.hpp"
20 #include "services.hpp"
21 
22 #include <memory>
23 #include <stdexcept>
24 #include <string>
25 #include <utility>
26 #include <vector>
27 
28 namespace phosphor::power::regulators
29 {
30 
31 // Forward declarations to avoid circular dependencies
32 class System;
33 
34 /**
35  * @class Chassis
36  *
37  * A chassis within the system.
38  *
39  * Chassis are large enclosures that can be independently powered off and on by
40  * the BMC.  Small and mid-sized systems may contain a single chassis.  In a
41  * large rack-mounted system, each drawer may correspond to a chassis.
42  *
43  * A C++ Chassis object only needs to be created if the physical chassis
44  * contains regulators that need to be configured or monitored.
45  */
46 class Chassis
47 {
48   public:
49     // Specify which compiler-generated methods we want
50     Chassis() = delete;
51     Chassis(const Chassis&) = delete;
52     Chassis(Chassis&&) = delete;
53     Chassis& operator=(const Chassis&) = delete;
54     Chassis& operator=(Chassis&&) = delete;
55     ~Chassis() = default;
56 
57     /**
58      * Constructor.
59      *
60      * Throws an exception if any of the input parameters are invalid.
61      *
62      * @param number Chassis number within the system.  Chassis numbers start at
63      *               1 because chassis 0 represents the entire system.
64      * @param devices Devices within this chassis, if any.  The vector should
65      *                contain regulator devices and any related devices required
66      *                to perform regulator operations.
67      */
68     explicit Chassis(unsigned int number,
69                      std::vector<std::unique_ptr<Device>> devices =
70                          std::vector<std::unique_ptr<Device>>{}) :
71         number{number},
72         devices{std::move(devices)}
73     {
74         if (number < 1)
75         {
76             throw std::invalid_argument{"Invalid chassis number: " +
77                                         std::to_string(number)};
78         }
79     }
80 
81     /**
82      * Adds the Device and Rail objects in this chassis to the specified IDMap.
83      *
84      * @param idMap mapping from IDs to the associated Device/Rail/Rule objects
85      */
86     void addToIDMap(IDMap& idMap);
87 
88     /**
89      * Clear any cached data about hardware devices.
90      */
91     void clearCache();
92 
93     /**
94      * Close the devices within this chassis, if any.
95      *
96      * @param services system services like error logging and the journal
97      */
98     void closeDevices(Services& services);
99 
100     /**
101      * Configure the devices within this chassis, if any.
102      *
103      * This method should be called during the boot before regulators are
104      * enabled.
105      *
106      * @param services system services like error logging and the journal
107      * @param system system that contains this chassis
108      */
109     void configure(Services& services, System& system);
110 
111     /**
112      * Returns the devices within this chassis, if any.
113      *
114      * The vector contains regulator devices and any related devices
115      * required to perform regulator operations.
116      *
117      * @return devices in chassis
118      */
119     const std::vector<std::unique_ptr<Device>>& getDevices() const
120     {
121         return devices;
122     }
123 
124     /**
125      * Returns the chassis number within the system.
126      *
127      * @return chassis number
128      */
129     unsigned int getNumber() const
130     {
131         return number;
132     }
133 
134     /**
135      * Monitors the sensors for the voltage rails produced by this chassis, if
136      * any.
137      *
138      * This method should be called once per second.
139      *
140      * @param services system services like error logging and the journal
141      * @param system system that contains the chassis
142      */
143     void monitorSensors(Services& services, System& system);
144 
145   private:
146     /**
147      * Chassis number within the system.
148      *
149      * Chassis numbers start at 1 because chassis 0 represents the entire
150      * system.
151      */
152     const unsigned int number{};
153 
154     /**
155      * Devices within this chassis, if any.
156      *
157      * The vector contains regulator devices and any related devices
158      * required to perform regulator operations.
159      */
160     std::vector<std::unique_ptr<Device>> devices{};
161 };
162 
163 } // namespace phosphor::power::regulators
164