Matt Spinler | 25515d5 | 2017-07-24 13:48:03 -0500 | [diff] [blame] | 1 | #pragma once |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 2 | #include "device.hpp" |
| 3 | |
Matt Spinler | 25515d5 | 2017-07-24 13:48:03 -0500 | [diff] [blame] | 4 | #include <phosphor-logging/log.hpp> |
| 5 | #include <sdbusplus/bus.hpp> |
| 6 | #include <sdbusplus/server.hpp> |
William A. Kennington III | 1a0c917 | 2018-10-18 17:57:49 -0700 | [diff] [blame] | 7 | #include <sdeventplus/clock.hpp> |
William A. Kennington III | e5a8b47 | 2018-10-18 00:40:04 -0700 | [diff] [blame] | 8 | #include <sdeventplus/event.hpp> |
William A. Kennington III | 1a0c917 | 2018-10-18 17:57:49 -0700 | [diff] [blame] | 9 | #include <sdeventplus/utility/timer.hpp> |
Matt Spinler | 25515d5 | 2017-07-24 13:48:03 -0500 | [diff] [blame] | 10 | |
| 11 | namespace witherspoon |
| 12 | { |
| 13 | namespace power |
| 14 | { |
| 15 | |
| 16 | using 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 | */ |
| 25 | class DeviceMonitor |
| 26 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 27 | 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 Spinler | 25515d5 | 2017-07-24 13:48:03 -0500 | [diff] [blame] | 34 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 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, 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 Spinler | 25515d5 | 2017-07-24 13:48:03 -0500 | [diff] [blame] | 48 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 49 | /** |
| 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 Spinler | 25515d5 | 2017-07-24 13:48:03 -0500 | [diff] [blame] | 56 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 57 | 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 Spinler | 25515d5 | 2017-07-24 13:48:03 -0500 | [diff] [blame] | 69 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 70 | /** |
| 71 | * The device to run the analysis on |
| 72 | */ |
| 73 | std::unique_ptr<Device> device; |
Matt Spinler | 25515d5 | 2017-07-24 13:48:03 -0500 | [diff] [blame] | 74 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 75 | /** |
| 76 | * The timer that runs fault check polls. |
| 77 | */ |
| 78 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer; |
Matt Spinler | 25515d5 | 2017-07-24 13:48:03 -0500 | [diff] [blame] | 79 | }; |
| 80 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 81 | } // namespace power |
| 82 | } // namespace witherspoon |