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 witherspoon
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     ~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     /**
50      * Starts the timer to monitor the device on an interval.
51      */
52     virtual int run()
53     {
54         return timer.get_event().loop();
55     }
56 
57   protected:
58     /**
59      * Analyzes the device for faults
60      *
61      * Runs in the timer callback
62      *
63      * Override if desired
64      */
65     virtual void analyze()
66     {
67         device->analyze();
68     }
69 
70     /**
71      * The device to run the analysis on
72      */
73     std::unique_ptr<Device> device;
74 
75     /**
76      * The timer that runs fault check polls.
77      */
78     sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer;
79 };
80 
81 } // namespace power
82 } // namespace witherspoon
83