blob: 0813737c6dabbda0a1df7c5e8a77ab841861de35 [file] [log] [blame]
Matt Spinler25515d52017-07-24 13:48:03 -05001#pragma once
2#include <phosphor-logging/log.hpp>
3#include <sdbusplus/bus.hpp>
4#include <sdbusplus/server.hpp>
William A. Kennington IIIe5a8b472018-10-18 00:40:04 -07005#include <sdeventplus/event.hpp>
Matt Spinler25515d52017-07-24 13:48:03 -05006#include "device.hpp"
Matt Spinler25515d52017-07-24 13:48:03 -05007#include "timer.hpp"
8
9namespace witherspoon
10{
11namespace power
12{
13
14using 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 */
23class 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,
William A. Kennington IIIe5a8b472018-10-18 00:40:04 -070042 const sdeventplus::Event& e,
Matt Spinler25515d52017-07-24 13:48:03 -050043 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);
William A. Kennington IIIe5a8b472018-10-18 00:40:04 -070060 return event.loop();
Matt Spinler25515d52017-07-24 13:48:03 -050061 }
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 /**
William A. Kennington IIIe5a8b472018-10-18 00:40:04 -070083 * The event loop used by the timer
Matt Spinler25515d52017-07-24 13:48:03 -050084 */
William A. Kennington IIIe5a8b472018-10-18 00:40:04 -070085 sdeventplus::Event event;
Matt Spinler25515d52017-07-24 13:48:03 -050086
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}