1 #pragma once
2 
3 #include "pmbus.hpp"
4 #include "types.hpp"
5 #include "utility.hpp"
6 
7 #include <sdbusplus/bus/match.hpp>
8 
9 namespace phosphor::power::psu
10 {
11 
12 /**
13  * @class PowerSupply
14  * Represents a PMBus power supply device.
15  */
16 class PowerSupply
17 {
18   public:
19     PowerSupply() = delete;
20     PowerSupply(const PowerSupply&) = delete;
21     PowerSupply(PowerSupply&&) = delete;
22     PowerSupply& operator=(const PowerSupply&) = delete;
23     PowerSupply& operator=(PowerSupply&&) = delete;
24     ~PowerSupply() = default;
25 
26     /**
27      * @param[in] invpath - String for inventory path to use
28      * @param[in] i2cbus - The bus number this power supply is on
29      * @param[in] i2caddr - The 16-bit I2C address of the power supply
30      */
31     PowerSupply(sdbusplus::bus::bus& bus, const std::string& invpath,
32                 std::uint8_t i2cbus, const std::string& i2caddr) :
33         bus(bus),
34         inventoryPath(invpath),
35         pmbusIntf(phosphor::pmbus::createPMBus(i2cbus, i2caddr))
36     {
37         // Setup the functions to call when the D-Bus inventory path for the
38         // Present property changes.
39         presentMatch = std::make_unique<sdbusplus::bus::match_t>(
40             bus,
41             sdbusplus::bus::match::rules::propertiesChanged(inventoryPath,
42                                                             INVENTORY_IFACE),
43             [this](auto& msg) { this->inventoryChanged(msg); });
44         presentAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
45             bus,
46             sdbusplus::bus::match::rules::interfacesAdded() +
47                 sdbusplus::bus::match::rules::path_namespace(inventoryPath),
48             [this](auto& msg) { this->inventoryChanged(msg); });
49         // Get the current state of the Present property.
50         updatePresence();
51     }
52 
53     phosphor::pmbus::PMBusBase& getPMBus()
54     {
55         return *pmbusIntf;
56     }
57 
58     /**
59      * Power supply specific function to analyze for faults/errors.
60      *
61      * Various PMBus status bits will be checked for fault conditions.
62      * If a certain fault bits are on, the appropriate error will be
63      * committed.
64      */
65     void analyze();
66 
67     /**
68      * Write PMBus CLEAR_FAULTS
69      *
70      * This function will be called in various situations in order to clear
71      * any fault status bits that may have been set, in order to start over
72      * with a clean state. Presence changes and power state changes will
73      * want to clear any faults logged.
74      */
75     void clearFaults();
76 
77     /**
78      * @brief Adds properties to the inventory.
79      *
80      * Reads the values from the device and writes them to the
81      * associated power supply D-Bus inventory object.
82      *
83      * This needs to be done on startup, and each time the presence
84      * state changes.
85      *
86      * Properties added:
87      * - Serial Number
88      * - Part Number
89      * - CCIN (Customer Card Identification Number) - added as the Model
90      * - Firmware version
91      */
92     void updateInventory()
93     {
94     }
95 
96     /**
97      * @brief Accessor function to indicate present status
98      */
99     bool isPresent() const
100     {
101         return present;
102     }
103 
104     /**
105      * @brief Returns true if a fault was found.
106      */
107     bool isFaulted() const
108     {
109         return faultFound;
110     }
111 
112     /**
113      * @brief Returns true if INPUT fault occurred.
114      */
115     bool hasInputFault() const
116     {
117         return inputFault;
118     }
119 
120     /**
121      * @brief Returns true if MFRSPECIFIC occurred.
122      */
123     bool hasMFRFault() const
124     {
125         return mfrFault;
126     }
127 
128     /**
129      * @brief Returns true if VIN_UV_FAULT occurred.
130      */
131     bool hasVINUVFault() const
132     {
133         return vinUVFault;
134     }
135 
136   private:
137     /** @brief systemd bus member */
138     sdbusplus::bus::bus& bus;
139 
140     /** @brief True if a fault has already been found and not cleared */
141     bool faultFound = false;
142 
143     /** @brief True if bit 5 of STATUS_WORD high byte is on. */
144     bool inputFault = false;
145 
146     /** @brief True if bit 4 of STATUS_WORD high byte is on. */
147     bool mfrFault = false;
148 
149     /** @brief True if bit 3 of STATUS_WORD low byte is on. */
150     bool vinUVFault = false;
151 
152     /**
153      * @brief D-Bus path to use for this power supply's inventory status.
154      **/
155     std::string inventoryPath;
156 
157     /** @brief True if the power supply is present. */
158     bool present = false;
159 
160     /** @brief D-Bus match variable used to subscribe to Present property
161      * changes.
162      **/
163     std::unique_ptr<sdbusplus::bus::match_t> presentMatch;
164 
165     /** @brief D-Bus match variable used to subscribe for Present property
166      * interface added.
167      */
168     std::unique_ptr<sdbusplus::bus::match_t> presentAddedMatch;
169 
170     /**
171      * @brief Pointer to the PMBus interface
172      *
173      * Used to read or write to/from PMBus power supply devices.
174      */
175     std::unique_ptr<phosphor::pmbus::PMBusBase> pmbusIntf;
176 
177     /**
178      *  @brief Updates the presence status by querying D-Bus
179      *
180      * The D-Bus inventory properties for this power supply will be read to
181      * determine if the power supply is present or not and update this
182      * object's present member variable to reflect current status.
183      **/
184     void updatePresence();
185 
186     /**
187      * @brief Callback for inventory property changes
188      *
189      * Process change of Present property for power supply.
190      *
191      * @param[in]  msg - Data associated with Present change signal
192      **/
193     void inventoryChanged(sdbusplus::message::message& msg);
194 };
195 
196 } // namespace phosphor::power::psu
197