blob: 3bde1b5907896b5c3d17d7e90e0bda886dfa3d3e [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 III1a0c9172018-10-18 17:57:49 -07005#include <sdeventplus/clock.hpp>
William A. Kennington IIIe5a8b472018-10-18 00:40:04 -07006#include <sdeventplus/event.hpp>
William A. Kennington III1a0c9172018-10-18 17:57:49 -07007#include <sdeventplus/utility/timer.hpp>
Matt Spinler25515d52017-07-24 13:48:03 -05008#include "device.hpp"
Matt Spinler25515d52017-07-24 13:48:03 -05009
10namespace witherspoon
11{
12namespace power
13{
14
15using 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 */
24class 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,
William A. Kennington IIIe5a8b472018-10-18 00:40:04 -070043 const sdeventplus::Event& e,
Matt Spinler25515d52017-07-24 13:48:03 -050044 std::chrono::milliseconds i) :
45 device(std::move(d)),
William A. Kennington III1a0c9172018-10-18 17:57:49 -070046 timer(e, std::bind(&DeviceMonitor::analyze, this), i)
Matt Spinler25515d52017-07-24 13:48:03 -050047 {
48 }
49
50 /**
51 * Starts the timer to monitor the device on an interval.
52 */
53 virtual int run()
54 {
William A. Kennington III1a0c9172018-10-18 17:57:49 -070055 return timer.get_event().loop();
Matt Spinler25515d52017-07-24 13:48:03 -050056 }
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 /**
Matt Spinler25515d52017-07-24 13:48:03 -050078 * The timer that runs fault check polls.
79 */
William A. Kennington III1a0c9172018-10-18 17:57:49 -070080 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer;
Matt Spinler25515d52017-07-24 13:48:03 -050081};
82
83}
84}