1 #pragma once
2 #include <sdbusplus/bus/match.hpp>
3 #include "device.hpp"
4 #include "pmbus.hpp"
5 #include "timer.hpp"
6 
7 namespace witherspoon
8 {
9 namespace power
10 {
11 namespace psu
12 {
13 
14 namespace sdbusRule = sdbusplus::bus::match::rules;
15 
16 /**
17  * @class PowerSupply
18  * Represents a PMBus power supply device.
19  */
20 class PowerSupply : public Device
21 {
22     public:
23         PowerSupply() = delete;
24         PowerSupply(const PowerSupply&) = delete;
25         PowerSupply(PowerSupply&&) = default;
26         PowerSupply& operator=(const PowerSupply&) = default;
27         PowerSupply& operator=(PowerSupply&&) = default;
28         ~PowerSupply() = default;
29 
30         /**
31          * Constructor
32          *
33          * @param[in] name - the device name
34          * @param[in] inst - the device instance
35          * @param[in] objpath - the path to monitor
36          * @param[in] invpath - the inventory path to use
37          * @param[in] bus - D-Bus bus object
38          * @param[in] e - event object
39          * @param[in] t - time to allow power supply to assert PG#
40          */
41         PowerSupply(const std::string& name, size_t inst,
42                     const std::string& objpath,
43                     const std::string& invpath,
44                     sdbusplus::bus::bus& bus,
45                     event::Event& e,
46                     std::chrono::seconds& t);
47 
48         /**
49          * Power supply specific function to analyze for faults/errors.
50          *
51          * Various PMBus status bits will be checked for fault conditions.
52          * If a certain fault bits are on, the appropriate error will be
53          * committed.
54          */
55         void analyze() override;
56 
57         /**
58          * Write PMBus CLEAR_FAULTS
59          *
60          * This function will be called in various situations in order to clear
61          * any fault status bits that may have been set, in order to start over
62          * with a clean state. Presence changes and power state changes will
63          * want to clear any faults logged.
64          */
65         void clearFaults() override;
66 
67     private:
68         /**
69          * The path to use for reading various PMBus bits/words.
70          */
71         std::string monitorPath;
72 
73         /**
74          * @brief Pointer to the PMBus interface
75          *
76          * Used to read out of or write to the /sysfs tree(s) containing files
77          * that a device driver monitors the PMBus interface to the power
78          * supplies.
79          */
80         witherspoon::pmbus::PMBus pmbusIntf;
81 
82         /**
83          * @brief D-Bus path to use for this power supply's inventory status.
84          */
85         std::string inventoryPath;
86 
87         /** @brief Connection for sdbusplus bus */
88         sdbusplus::bus::bus& bus;
89 
90         /** @brief True if the power supply is present. */
91         bool present = false;
92 
93         /** @brief Used to subscribe to D-Bus property changes to Present **/
94         std::unique_ptr<sdbusplus::bus::match_t> presentMatch;
95 
96         /** @brief True if the power is on. */
97         bool powerOn = false;
98 
99         /** @brief The sd_event structure used by the power on timer. */
100         event::Event& event;
101 
102         /**
103          * @brief Interval to setting powerOn to true.
104          *
105          * The amount of time to wait from power state on to setting the
106          * internal powerOn state to true. The amount of time the power supply
107          * is allowed to delay setting DGood/PG#.
108          */
109         std::chrono::seconds powerOnInterval;
110 
111         /**
112          * @brief Timer used to delay setting the internal powerOn state.
113          *
114          * The timer used to do the callback after the power state has been on
115          * long enough.
116          */
117         Timer powerOnTimer;
118 
119         /** @brief Used to subscribe to D-Bus power on state changes **/
120         std::unique_ptr<sdbusplus::bus::match_t> powerOnMatch;
121 
122         /**
123          * @brief Has a PMBus read failure already been logged?
124          */
125         bool readFailLogged = false;
126 
127         /**
128          * @brief Set to true when a VIN UV fault has been detected
129          *
130          * This is the VIN_UV_FAULT bit in the low byte from the STATUS_WORD
131          * command response.
132          */
133         bool vinUVFault = false;
134 
135         /**
136          * @brief Set to true when an input fault or warning is detected
137          *
138          * This is the "INPUT FAULT OR WARNING" bit in the high byte from the
139          * STATUS_WORD command response.
140          */
141         bool inputFault = false;
142 
143         /** @brief Callback for inventory property changes
144          *
145          * Process change of Present property for power supply.
146          *
147          * @param[in]  msg - Data associated with Present change signal
148          *
149          */
150         void inventoryChanged(sdbusplus::message::message& msg);
151 
152         /**
153          * Updates the presence status by querying D-Bus
154          *
155          * The D-Bus inventory properties for this power supply will be read to
156          * determine if the power supply is present or not and update this
157          * objects present member variable to reflect current status.
158          */
159         void updatePresence();
160 
161         /**
162          * @brief Updates the poweredOn status by querying D-Bus
163          *
164          * The D-Bus property for the sytem power state will be read to
165          * determine if the system is powered on or not.
166          */
167         void updatePowerState();
168 
169         /** @brief Callback for power state property changes
170          * Process changes to the powered on stat property for the system.
171          *
172          * @param[in] msg - Data associated with the power state signal
173          */
174         void powerStateChanged(sdbusplus::message::message& msg);
175 
176 };
177 
178 }
179 }
180 }
181