1 #pragma once
2 
3 #include "power_interface.hpp"
4 
5 #include <gpiod.hpp>
6 #include <sdbusplus/bus.hpp>
7 #include <sdbusplus/message.hpp>
8 #include <sdbusplus/server/object.hpp>
9 #include <sdeventplus/clock.hpp>
10 #include <sdeventplus/event.hpp>
11 #include <sdeventplus/utility/timer.hpp>
12 
13 #include <chrono>
14 
15 namespace phosphor::power::sequencer
16 {
17 
18 using PowerObject = sdbusplus::server::object::object<PowerInterface>;
19 
20 /**
21  * @class PowerControl
22  * This class implements GPIO control of power on / off, and monitoring of the
23  * chassis power good.
24  */
25 class PowerControl : public PowerObject
26 {
27   public:
28     PowerControl() = delete;
29     PowerControl(const PowerControl&) = delete;
30     PowerControl& operator=(const PowerControl&) = delete;
31     PowerControl(PowerControl&&) = delete;
32     PowerControl& operator=(PowerControl&&) = delete;
33     ~PowerControl() = default;
34 
35     /**
36      * Creates a controller object for power on and off.
37      * @param[in] bus D-Bus bus object
38      * @param[in] event event object
39      */
40     PowerControl(sdbusplus::bus::bus& bus, const sdeventplus::Event& event);
41 
42     /** @copydoc PowerInterface::getPgood() */
43     int getPgood() const override;
44 
45     /** @copydoc PowerInterface::getPgoodTimeout() */
46     int getPgoodTimeout() const override;
47 
48     /** @copydoc PowerInterface::getState() */
49     int getState() const override;
50 
51     /** @copydoc PowerInterface::setPgoodTimeout() */
52     void setPgoodTimeout(int timeout) override;
53 
54     /** @copydoc PowerInterface::setState() */
55     void setState(int state) override;
56 
57   private:
58     /**
59      * The D-Bus bus object
60      */
61     sdbusplus::bus::bus& bus;
62 
63     /**
64      * Indicates if a state transistion is taking place
65      */
66     bool inStateTransition{false};
67 
68     /**
69      * Power good
70      */
71     int pgood{0};
72 
73     /**
74      * GPIO line object for chassis power good
75      */
76     gpiod::line pgoodLine;
77 
78     /**
79      * Power good timeout constant
80      */
81     static constexpr std::chrono::seconds pgoodTimeout{
82         std::chrono::seconds(10)};
83 
84     /**
85      * Point in time at which power good timeout will take place
86      */
87     std::chrono::time_point<std::chrono::steady_clock> pgoodTimeoutTime;
88 
89     /**
90      * Poll interval constant
91      */
92     static constexpr std::chrono::milliseconds pollInterval{
93         std::chrono::milliseconds(3000)};
94 
95     /**
96      * GPIO line object for power-on / power-off control
97      */
98     gpiod::line powerControlLine;
99 
100     /**
101      * Power state
102      */
103     int state{0};
104 
105     /**
106      * Power good timeout
107      */
108     std::chrono::seconds timeout{pgoodTimeout};
109 
110     /**
111      * Timer to poll the pgood
112      */
113     sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer;
114 
115     /**
116      * Polling method for monitoring the system power good
117      */
118     void pollPgood();
119 
120     /**
121      * Set up GPIOs
122      */
123     void setUpGpio();
124 };
125 
126 } // namespace phosphor::power::sequencer
127