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