blob: 3214afbedce0d8d057cc5decf97ff8c0a7c138fe [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>
5#include "device.hpp"
6#include "event.hpp"
7#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,
42 event::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
61 auto r = sd_event_loop(event.get());
62 if (r < 0)
63 {
64 log<level::ERR>("sd_event_loop() failed",
65 entry("ERROR=%s", strerror(-r)));
66 }
67
68 return r;
69 }
70
71 protected:
72
73 /**
74 * Analyzes the device for faults
75 *
76 * Runs in the timer callback
77 *
78 * Override if desired
79 */
80 virtual void analyze()
81 {
82 device->analyze();
83 }
84
85 /**
86 * The device to run the analysis on
87 */
88 std::unique_ptr<Device> device;
89
90 /**
91 * The sd_event structure used by the timer
92 */
93 event::Event& event;
94
95 /**
96 * The polling interval in milliseconds
97 */
98 std::chrono::milliseconds interval;
99
100 /**
101 * The timer that runs fault check polls.
102 */
103 Timer timer;
104};
105
106}
107}