xref: /openbmc/phosphor-power/device_monitor.hpp (revision 8b38b177315d40f1f9bc1e4d0784d45ea17cbd5c)
1 #pragma once
2 #include "device.hpp"
3 
4 #include <sdbusplus/bus.hpp>
5 #include <sdbusplus/server.hpp>
6 #include <sdeventplus/clock.hpp>
7 #include <sdeventplus/event.hpp>
8 #include <sdeventplus/utility/timer.hpp>
9 
10 namespace phosphor
11 {
12 namespace power
13 {
14 
15 /**
16  * @class DeviceMonitor
17  *
18  * Monitors a power device for faults by calling Device::analyze()
19  * on an interval.  Do the monitoring by calling run().
20  * May be overridden to provide more functionality.
21  */
22 class DeviceMonitor
23 {
24   public:
25     DeviceMonitor() = delete;
26     virtual ~DeviceMonitor() = default;
27     DeviceMonitor(const DeviceMonitor&) = delete;
28     DeviceMonitor& operator=(const DeviceMonitor&) = delete;
29     DeviceMonitor(DeviceMonitor&&) = delete;
30     DeviceMonitor& operator=(DeviceMonitor&&) = delete;
31 
32     /**
33      * Constructor
34      *
35      * @param[in] d - device to monitor
36      * @param[in] e - event object
37      * @param[in] i - polling interval in ms
38      */
39     DeviceMonitor(std::unique_ptr<Device>&& d, const sdeventplus::Event& e,
40                   std::chrono::milliseconds i) :
41         device(std::move(d)),
42         timer(e, std::bind(&DeviceMonitor::analyze, this), i)
43     {}
44 
45     /**
46      * Starts the timer to monitor the device on an interval.
47      */
48     virtual int run()
49     {
50         return timer.get_event().loop();
51     }
52 
53   protected:
54     /**
55      * Analyzes the device for faults
56      *
57      * Runs in the timer callback
58      *
59      * Override if desired
60      */
61     virtual void analyze()
62     {
63         device->analyze();
64     }
65 
66     /**
67      * The device to run the analysis on
68      */
69     std::unique_ptr<Device> device;
70 
71     /**
72      * The timer that runs fault check polls.
73      */
74     sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer;
75 };
76 
77 } // namespace power
78 } // namespace phosphor
79