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