blob: 9f06a96eb27c1a248db41c306b5c70667750248e [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 <sdbusplus/bus.hpp>
5#include <sdbusplus/server.hpp>
William A. Kennington III1a0c9172018-10-18 17:57:49 -07006#include <sdeventplus/clock.hpp>
William A. Kennington IIIe5a8b472018-10-18 00:40:04 -07007#include <sdeventplus/event.hpp>
William A. Kennington III1a0c9172018-10-18 17:57:49 -07008#include <sdeventplus/utility/timer.hpp>
Matt Spinler25515d52017-07-24 13:48:03 -05009
Lei YUab093322019-10-09 16:43:22 +080010namespace phosphor
Matt Spinler25515d52017-07-24 13:48:03 -050011{
12namespace power
13{
14
Matt Spinler25515d52017-07-24 13:48:03 -050015/**
16 * @class DeviceMonitor
17 *
18 * Monitors a power device for faults by calling Device::analyze()
19 * on an interval. Do the monitoring by calling run().
20 * May be overridden to provide more functionality.
21 */
22class DeviceMonitor
23{
Matt Spinlerf0f02b92018-10-25 16:12:43 -050024 public:
25 DeviceMonitor() = delete;
Brad Bishop11cb6722019-09-03 14:30:06 -040026 virtual ~DeviceMonitor() = default;
Matt Spinlerf0f02b92018-10-25 16:12:43 -050027 DeviceMonitor(const DeviceMonitor&) = delete;
28 DeviceMonitor& operator=(const DeviceMonitor&) = delete;
29 DeviceMonitor(DeviceMonitor&&) = delete;
30 DeviceMonitor& operator=(DeviceMonitor&&) = delete;
Matt Spinler25515d52017-07-24 13:48:03 -050031
Matt Spinlerf0f02b92018-10-25 16:12:43 -050032 /**
33 * Constructor
34 *
35 * @param[in] d - device to monitor
36 * @param[in] e - event object
37 * @param[in] i - polling interval in ms
38 */
39 DeviceMonitor(std::unique_ptr<Device>&& d, const sdeventplus::Event& e,
40 std::chrono::milliseconds i) :
41 device(std::move(d)),
42 timer(e, std::bind(&DeviceMonitor::analyze, this), i)
Adriana Kobylak0c9a33d2021-09-13 18:05:09 +000043 {}
Matt Spinler25515d52017-07-24 13:48:03 -050044
Matt Spinlerf0f02b92018-10-25 16:12:43 -050045 /**
46 * Starts the timer to monitor the device on an interval.
47 */
48 virtual int run()
49 {
50 return timer.get_event().loop();
51 }
Matt Spinler25515d52017-07-24 13:48:03 -050052
Matt Spinlerf0f02b92018-10-25 16:12:43 -050053 protected:
54 /**
55 * Analyzes the device for faults
56 *
57 * Runs in the timer callback
58 *
59 * Override if desired
60 */
61 virtual void analyze()
62 {
63 device->analyze();
64 }
Matt Spinler25515d52017-07-24 13:48:03 -050065
Matt Spinlerf0f02b92018-10-25 16:12:43 -050066 /**
67 * The device to run the analysis on
68 */
69 std::unique_ptr<Device> device;
Matt Spinler25515d52017-07-24 13:48:03 -050070
Matt Spinlerf0f02b92018-10-25 16:12:43 -050071 /**
72 * The timer that runs fault check polls.
73 */
74 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer;
Matt Spinler25515d52017-07-24 13:48:03 -050075};
76
Matt Spinlerf0f02b92018-10-25 16:12:43 -050077} // namespace power
Lei YUab093322019-10-09 16:43:22 +080078} // namespace phosphor