blob: 191c2bc827d6a86ae6f9eab900e665370e4624cd [file] [log] [blame]
Matt Spinler25515d52017-07-24 13:48:03 -05001#pragma once
Matt Spinlerf0f02b92018-10-25 16:12:43 -05002#include "device.hpp"
3
Matt Spinler25515d52017-07-24 13:48:03 -05004#include <phosphor-logging/log.hpp>
5#include <sdbusplus/bus.hpp>
6#include <sdbusplus/server.hpp>
William A. Kennington III1a0c9172018-10-18 17:57:49 -07007#include <sdeventplus/clock.hpp>
William A. Kennington IIIe5a8b472018-10-18 00:40:04 -07008#include <sdeventplus/event.hpp>
William A. Kennington III1a0c9172018-10-18 17:57:49 -07009#include <sdeventplus/utility/timer.hpp>
Matt Spinler25515d52017-07-24 13:48:03 -050010
11namespace witherspoon
12{
13namespace power
14{
15
16using namespace phosphor::logging;
17
18/**
19 * @class DeviceMonitor
20 *
21 * Monitors a power device for faults by calling Device::analyze()
22 * on an interval. Do the monitoring by calling run().
23 * May be overridden to provide more functionality.
24 */
25class DeviceMonitor
26{
Matt Spinlerf0f02b92018-10-25 16:12:43 -050027 public:
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;
Matt Spinler25515d52017-07-24 13:48:03 -050034
Matt Spinlerf0f02b92018-10-25 16:12:43 -050035 /**
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, const sdeventplus::Event& e,
43 std::chrono::milliseconds i) :
44 device(std::move(d)),
45 timer(e, std::bind(&DeviceMonitor::analyze, this), i)
46 {
47 }
Matt Spinler25515d52017-07-24 13:48:03 -050048
Matt Spinlerf0f02b92018-10-25 16:12:43 -050049 /**
50 * Starts the timer to monitor the device on an interval.
51 */
52 virtual int run()
53 {
54 return timer.get_event().loop();
55 }
Matt Spinler25515d52017-07-24 13:48:03 -050056
Matt Spinlerf0f02b92018-10-25 16:12:43 -050057 protected:
58 /**
59 * Analyzes the device for faults
60 *
61 * Runs in the timer callback
62 *
63 * Override if desired
64 */
65 virtual void analyze()
66 {
67 device->analyze();
68 }
Matt Spinler25515d52017-07-24 13:48:03 -050069
Matt Spinlerf0f02b92018-10-25 16:12:43 -050070 /**
71 * The device to run the analysis on
72 */
73 std::unique_ptr<Device> device;
Matt Spinler25515d52017-07-24 13:48:03 -050074
Matt Spinlerf0f02b92018-10-25 16:12:43 -050075 /**
76 * The timer that runs fault check polls.
77 */
78 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer;
Matt Spinler25515d52017-07-24 13:48:03 -050079};
80
Matt Spinlerf0f02b92018-10-25 16:12:43 -050081} // namespace power
82} // namespace witherspoon