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