blob: ec0151cc494e5a18406bf85cc26ba951ab913f7e [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
Lei YUab093322019-10-09 16:43:22 +080011namespace phosphor
Matt Spinler25515d52017-07-24 13:48:03 -050012{
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;
Brad Bishop11cb6722019-09-03 14:30:06 -040029 virtual ~DeviceMonitor() = default;
Matt Spinlerf0f02b92018-10-25 16:12:43 -050030 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)
Adriana Kobylak0c9a33d2021-09-13 18:05:09 +000046 {}
Matt Spinler25515d52017-07-24 13:48:03 -050047
Matt Spinlerf0f02b92018-10-25 16:12:43 -050048 /**
49 * Starts the timer to monitor the device on an interval.
50 */
51 virtual int run()
52 {
53 return timer.get_event().loop();
54 }
Matt Spinler25515d52017-07-24 13:48:03 -050055
Matt Spinlerf0f02b92018-10-25 16:12:43 -050056 protected:
57 /**
58 * Analyzes the device for faults
59 *
60 * Runs in the timer callback
61 *
62 * Override if desired
63 */
64 virtual void analyze()
65 {
66 device->analyze();
67 }
Matt Spinler25515d52017-07-24 13:48:03 -050068
Matt Spinlerf0f02b92018-10-25 16:12:43 -050069 /**
70 * The device to run the analysis on
71 */
72 std::unique_ptr<Device> device;
Matt Spinler25515d52017-07-24 13:48:03 -050073
Matt Spinlerf0f02b92018-10-25 16:12:43 -050074 /**
75 * The timer that runs fault check polls.
76 */
77 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer;
Matt Spinler25515d52017-07-24 13:48:03 -050078};
79
Matt Spinlerf0f02b92018-10-25 16:12:43 -050080} // namespace power
Lei YUab093322019-10-09 16:43:22 +080081} // namespace phosphor