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